Httpwebrequest upload file / Multipart POST

spidertimo

Registered Member
Joined
May 7, 2010
Messages
91
Reaction score
58
Hello,

I'm programming a tool to upload pictures to my facebook group. Posting text to the group works like a charm but I don't know how to upload pictures with httpwebrequest.

Can someone help?

Thank you!
 
Hello,

I'm programming a tool to upload pictures to my facebook group. Posting text to the group works like a charm but I don't know how to upload pictures with httpwebrequest.

Can someone help?

Thank you!
What have you tried that didn't work? A simple google search shows up tons of results.
 
I get this from HTTP-Liveheaders (I used an add-me page for testing):
1.png
sorry the source contains URLs and I'm not allowed to post URLs...

I found a code on the internet for uploading an image and edited it:
2.PNG


I get an error on this line at the end:
RequestStream.Close()


Error:
The request was aborted: The request was canceled ..


Can someone help me?
 
HttpClient .NET 4 or above, has multipart post implemented.It still requires some tuning, but you don't have to deal with the post string directly.

1) Download fiddler -> http://www.telerik.com/fiddler
2) Upload a picture manually. You can create an empty file and rename the extension to *.jpeg - fiddler won't read the strings after the binary data, but with an empty file you will see the entire post data.
3) Mimic the headers and post data.
4) Profit.
 
I mimiced the headers and post data. Now all looks the same but the "image code". After this lines:
Code:
Content-Disposition: form-data; name="file1"; filename="156928_318983771550461_1084321640_n.jpg"
Content-Type: image/jpeg

it transfers this insteed of the image-bytes:
Code:
d�

any ideas?
 
Last edited:
I'm also stuck trying to do this with httpwebrequest tweeting an image..

Here is what I've got: http://pastebin.com/8j6PXvGd

I compared that to firefox in fiddler and they both appear exactly the same yet this returns a 500 internal server error.
 
export the fiddler file with the request and I will put it together for you :)
 
Okay so I just compared side-by-side in windows and on the textview I noticed the first line was empty for my program.

Basically I changed
Code:
Dim boundarybytes As Byte() = System.Text.Encoding.ASCII.GetBytes(vbCr & vbLf & "--" & boundary & vbCr & vbLf)

to

Dim boundarybytes As Byte() = System.Text.Encoding.ASCII.GetBytes("--" & boundary & vbCr & vbLf)

and now it works. To OP, look at my code with this change - I'm sure it'll work the same for facebook once the values are changed.
 
Back
Top