Picnik Forums
   Home   Help Search Login Register Picnik.com  
 
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Pages: [1]
Print
Author Topic: vb.net or c# example of posting from server to Picnik???  (Read 24924 times)
revnique
posted: January 30, 2008, 08:32:52 AM

Do you guys have any examples of someone doing a server side multipart post to your site using vb.net or c#? or do you have any clients that would be willing to provide some samples? the code you have posted from stewsterl was very helpful for GETTING the image now i just need to POST the image to you properly.

thank you

-rev
stewsterl
Reply #1 posted: January 30, 2008, 08:55:27 AM

Hello... Smiley

I posted code for both VB.NET and C# at this forum entry:

Getting Images Out in ASP.NET  (stewsterl)
http://forums.picnik.com/index.php/topic,113.0.html

revnique
Reply #2 posted: January 30, 2008, 09:08:53 AM

sorry  Sad stewsterl it's not the same thing. i need the server side POST. not the RECEIVE part. your code is great for getting the file back from Picnik Grin but how are you SENDING them the file? are you clicking on the "browse..." button on your webpage? if you are that won't help me. I need this piece of psudocode to work.

SendFileToPicknik("C:\testdocs\Creek.jpg")

so what would the code to this function look like?

any ideas anyone???
stewsterl
Reply #3 posted: January 30, 2008, 09:40:32 AM

Just to make sure I understand you correctly.

You want to send an image to Picnik via server side code without a user click?

There are a few ways to do this.

Response.redirect("url")

response.write("<script>");
response.write("window.open('page.html','_blank')");
response.write("</script>");

The purpose of picnik is to allow the user to modify the image. Therefore you will need a page to open so picnik can be loaded into it.

Not sure if there are auto photo editing parameters that can be added to the submit string.

stewsterl
Reply #4 posted: January 30, 2008, 09:52:18 AM

Have your server-side code modify your url submit string.

imports system.io

Dim sw As StringWriter = New StringWriter

sw.WriteLine("href="http://www.picnik.com/service?_apikey=YourKey")
sw.WriteLine("") and so on...

dim urlToSubmit as string = sw.ToString

SendFileToPicknik(urlToSubmit)



revnique
Reply #5 posted: January 30, 2008, 01:50:28 PM

sorry again stewsterl you are not understanding me.

I have a site where people can upload files to. I save them on the server and when the user logs in i give them access to this file by going to my database and getting the file location like "c:\username\photos\cat.jpg"

i have to make this function call work

SendFileToPicknik("C:\testdocs\Creek.jpg")



Code Listed Below

Imports System.Net
Imports System.IO

Partial Class picnik : Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SendFileToPicknik("C:\testdocs\Creek.jpg")
    End Sub


    Sub SendFileToPicknik(ByVal sFullPath As String)
        Dim sPicnikURL As String = "http://www.picnik.com/service/"
        Dim sApiKey As String = "hidden"
        Dim sFileName As String = Path.GetFileName(sFullPath)
        Dim sFileNameForSaving As String = sFileName
        Dim fStream As FileStream
        Dim sCallingURL As String = Request.Url.AbsoluteUri.ToString

        Try
            Dim webrequest As HttpWebRequest = HttpWebRequest.Create(sPicnikURL)

            'Create a boundary item for use in the header
            Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")

            webrequest.Method = "POST"
            webrequest.KeepAlive = True
            webrequest.Headers.Add("Keep-Alive", "300")
            webrequest.ContentType = "multipart/form-data; boundary=" & boundary
            webrequest.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
            webrequest.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
            webrequest.Headers.Add("Accept-Encoding", "gzip, deflate")
            webrequest.Headers.Add("Accept-Language", "en-us,en;q=0.5")
            webrequest.Referer = sCallingURL
            webrequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"
            webrequest.Expect = ""

            ' Build up the post message header
            Dim sb As New StringBuilder


            'File Details
            sb.Append("--")
            sb.Append(boundary)
            sb.Append(vbCrLf)
            sb.Append("Content-Disposition: form-data; name=""")
            sb.Append("image_file")
            sb.Append("""; filename=""")
            sb.Append(sFileName) 'Replace with the name of your file
            sb.Append("""")
            sb.Append(vbCrLf)
            sb.Append("Content-Type: ")
            sb.Append("image/jpeg") 'This can be left blank if you are unsure what type to use
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)




            'Store this seperately to the rest of the header, it must appear before the file contents (see below)
            Dim fileHeader As String = sb.ToString()
            Dim fileHeaderBytes As Byte() = Encoding.UTF8.GetBytes(fileHeader)

            'Create the rest of the post header
            sb = New StringBuilder()


            'FileName attribute
            sb.Append(vbCrLf)
            sb.Append("--")
            sb.Append(boundary)
            sb.Append(vbCrLf)
            sb.Append("Content-Disposition: form-data; name=""")
            sb.Append("_import")
            sb.Append("""")
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)
            sb.Append("image_file")

            '_returntype
            sb.Append(vbCrLf)
            sb.Append("--")
            sb.Append(boundary)
            sb.Append(vbCrLf)
            sb.Append("Content-Disposition: form-data; name=""")
            sb.Append("_returntype")
            sb.Append("""")
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)
            sb.Append("text")

            '_export_field
            sb.Append(vbCrLf)
            sb.Append("--")
            sb.Append(boundary)
            sb.Append(vbCrLf)
            sb.Append("Content-Disposition: form-data; name=""")
            sb.Append("_export_field")
            sb.Append("""")
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)
            sb.Append("file")

            '_apikey
            sb.Append(vbCrLf)
            sb.Append("--")
            sb.Append(boundary)
            sb.Append(vbCrLf)
            sb.Append("Content-Disposition: form-data; name=""")
            sb.Append("_apikey")
            sb.Append("""")
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)
            sb.Append("hidden")

            'close the header
            sb.Append(vbCrLf)
            sb.Append("--")
            sb.Append(boundary)
            sb.Append("--")

            'Store the remaining header as a string and convert to a byte array
            Dim postHeader As String = sb.ToString()
            Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader)

            'Open the file
            fStream = New FileStream(sFullPath, FileMode.Open, FileAccess.Read, FileShare.None)
            Dim bytesRead As Integer = 0
            Dim buffer(fStream.Length) As Byte
            bytesRead = fStream.Read(buffer, 0, buffer.Length)

            'Close the file stream
            fStream.Close()

            'Get the length of the header.
            'This is NOT the length of the strings, but the length of their corresponding byte array
            Dim length As Long = fileHeaderBytes.Length + buffer.Length + postHeaderBytes.Length

            'Set content length
            webrequest.ContentLength = length

            'Open the request stream
            Dim requestStream As Stream = webrequest.GetRequestStream()

            'WRITE INFO INTO RESPONSE STREAM
            'write FileHeader to stream
            requestStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length)

            'write File to stream
            requestStream.Write(buffer, 0, buffer.Length)

            'write remaning header
            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)

            'get response
            Dim aResponse As WebResponse = webrequest.GetResponse()

            Dim url As String = aResponse.ResponseUri.AbsoluteUri.ToString

            Response.Redirect(url)
        Catch ex As Exception
            'Handle the error accordingly
            Throw ex
        Finally
            If Not IsNothing(fStream) Then
                fStream.Close()
            End If
        End Try
    End Sub
End Class
revnique
Reply #6 posted: January 30, 2008, 01:58:31 PM

Assuming you put in a proper apikey the picnik server times out when i make a post. i w written the request back to the hard so i could inspect it and it looks almost identical to what the browser sends. this is the request i am sending to picnik.

POST /testWeb/getPost.aspx HTTP/1.1
Keep-Alive: 300
Content-Length: 265557
Content-Type: multipart/form-data; boundary=---------------------------8ca3165c9b20100
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: en-us,en;q=0.5
Expect: 100-continue
Host: localhost:51144
Referer: http://localhost:51144/testWeb/picnik.aspx
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11

-----------------------------8ca3165c9b20100
Content-Disposition: form-data; name="image_file"; filename="Creek.jpg"
Content-Type: image/jpeg

11011-I OMITTED THE BINARY FILE CONTENT HERE-11011

-----------------------------8ca3165c9b20100
Content-Disposition: form-data; name="_import"

image_file
-----------------------------8ca3165c9b20100
Content-Disposition: form-data; name="_returntype"

text
-----------------------------8ca3165c9b20100
Content-Disposition: form-data; name="_export_field"

file
-----------------------------8ca3165c9b20100
Content-Disposition: form-data; name="_apikey"

65bQQQQQQQQQQQQQQQQQQQQQQQ
-----------------------------8ca3165c9b20100--
« Last Edit: February 01, 2008, 09:12:22 AM by revnique »
tmedhurst
Reply #7 posted: January 31, 2008, 06:32:08 AM

Hiya, I'm also writing a C# client.. The end HTTP request is below. Unfortunately I never get a response from the picnik servers (just a timeout  Sad )

But when I create a simple html form and post it the request *does* work :S

Can anyone help? Here is the request that *doesn't* work...
Many Thanks!
Tom


POST /service/ HTTP/1.1
Content-Type: multipart/form-data; boundary=-----------------------------7d837a2a30a44
Referer: http://localhost/picnik.htm
Accept: */*
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: en-gb;en-us,en;q=0.5
Host: www.picnik.com
Content-Length: 27847
Expect: 100-continue
Proxy-Connection: Keep-Alive


-----------------------------7d837a2a30a44
Content-Disposition: form-data; name="_apikey"

{MY_API_KEY}
-----------------------------7d837a2a30a44
Content-Disposition: form-data; name="_export_field"

file
-----------------------------7d837a2a30a44
Content-Disposition: form-data; name="_import"

image_file
-----------------------------7d837a2a30a44
Content-Disposition: form-data; name="_returntype"

text
-----------------------------7d837a2a30a44
Content-Disposition: form-data; name="image_file"; filename="small_logo.png"

{PNGDATA}
-----------------------------7d837a2a30a44
bitnik official jjhuff
(bitnik)

Reply #8 posted: January 31, 2008, 10:58:14 AM

Just so I can get all this straight: Are you trying to build a desktop C# program that uses Picnik for editing?
We're going to do some more testing on our end to make sure we haven't broken anything.
You might fire up fiddler/http analyzer just to make sure that your content type is exact.
Also, can you send me your IP address so we can check our server logs?

Thanks!
stewsterl
Reply #9 posted: January 31, 2008, 01:04:13 PM

Ahhh! I see. Sorry for the confusion. Huh

I worked on this a little and I to cannot get a response either... Tried a few things with no luck.
Kind of frustrating... Angry
tmedhurst
Reply #10 posted: January 31, 2008, 02:40:48 PM

Thanks jjhyff,
Yes this is a desktop C# app for editing images in Picnik.

The HTTP trace was from Fiddler itself.. so I am quite confident what I posted is what was actually sent.

I'm sorry but I can't give you the IP address as I was on a public Wifi network and now I'm at home Sad

Thanks for such a quick response!!
Tom
bitnik official jjhuff
(bitnik)

Reply #11 posted: February 05, 2008, 03:43:10 PM

Hey, sorry for the delayed response...we've been pretty busy over here.

I see a bunch of your requests timing out waiting for data, so my guess is that something isn't right with your content length, i.e. Content-Length is larger than the actual amount of data that you're sending.
george.talusan
Reply #12 posted: March 30, 2008, 03:03:40 PM

http://code.google.com/p/picnik-sharp/

I've created some C# bindings for picnik.com.  It's fairly simple.  Create the request, send the file, pull it back.
bonave
Reply #13 posted: July 17, 2008, 03:33:00 AM

Do you guys have any examples of someone doing a server side multipart post to your site using vb.net or c#? or do you have any clients that would be willing to provide some samples? the code you have posted from stewsterl was very helpful for GETTING the image now i just need to POST the image to you properly.

thank you

-rev


        Dim inStream As StreamReader
        Dim webRequest As WebRequest
        Dim webresponse As WebResponse
        webRequest = webRequest.Create(url)
        webresponse = webRequest.GetResponse()
        inStream = New StreamReader(webresponse.GetResponseStream())
        content = inStream.ReadToEnd()


try it

http://vb.net-informations.com/communications/vb.net_read_url.htm

bonave






qualitycpp
Reply #14 posted: September 13, 2009, 09:04:30 AM

        Dim inStream As StreamReader
        Dim webRequest As WebRequest
        Dim webresponse As WebResponse
        webRequest = webRequest.Create(url)
        webresponse = webRequest.GetResponse()
        inStream = New StreamReader(webresponse.GetResponseStream())
        content = inStream.ReadToEnd()

I don`t understand this part of code... Huh for what is he need?

ihijazi
Reply #15 posted: September 19, 2009, 07:13:58 AM

So anyone solved the timeout issue?

I've been trying my ways to get around it but with no luck, then i found the sharp picnik project online, and thought that "it's a done deal", but also with no luck. But I'd like to say it's well organized and make like much easier.


What I'm saying, I've tried almost everything, but the timeout error is still there!!

Anynody??! Any hint?
Pages: [1]
Print
 
Jump to:  

Powered by SMF 1.1 RC2 | SMF © 2001-2005, Lewis Media | © 2006-7 Picnik, Inc.