1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Httpwebrequest upload file / Multipart POST

Discussion in 'Visual Basic .NET' started by spidertimo, Apr 21, 2014.

  1. spidertimo

    spidertimo Registered Member

    Joined:
    May 7, 2010
    Messages:
    90
    Likes Received:
    58
    Location:
    Germany
    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!
     
  2. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    What have you tried that didn't work? A simple google search shows up tons of results.
     
  3. MoneyIsTheGod

    MoneyIsTheGod Power Member

    Joined:
    Oct 26, 2013
    Messages:
    618
    Likes Received:
    378
    Occupation:
    Software Developer
    Home Page:
    What codes did you write to do it? Paste it here and maybe I can help.
     
  4. Macthetrix

    Macthetrix Regular Member

    Joined:
    Feb 21, 2013
    Messages:
    379
    Likes Received:
    390
    Occupation:
    The Boss
    Location:
    Silicon Valley
    No one can help you if you dont give more details
     
  5. spidertimo

    spidertimo Registered Member

    Joined:
    May 7, 2010
    Messages:
    90
    Likes Received:
    58
    Location:
    Germany
    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?
     
  6. Macthetrix

    Macthetrix Regular Member

    Joined:
    Feb 21, 2013
    Messages:
    379
    Likes Received:
    390
    Occupation:
    The Boss
    Location:
    Silicon Valley
    Use Fiddler man and not some crappy chrome extension
     
  7. innosoft

    innosoft Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 25, 2008
    Messages:
    1,633
    Likes Received:
    639
    Occupation:
    Software Developer, SEO
    Location:
    Office
    Home Page:
    u need to change content type.
     
  8. spidertimo

    spidertimo Registered Member

    Joined:
    May 7, 2010
    Messages:
    90
    Likes Received:
    58
    Location:
    Germany
    I added this line:
    Code:
    request.ContentType = "Content-Type: multipart/form-data; " & boundary2
    but same error :/
     
  9. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    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.
     
  10. spidertimo

    spidertimo Registered Member

    Joined:
    May 7, 2010
    Messages:
    90
    Likes Received:
    58
    Location:
    Germany
    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: Apr 22, 2014
  11. gtothem

    gtothem Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 22, 2011
    Messages:
    236
    Likes Received:
    296
    Occupation:
    Coder
    Home Page:
    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.
     
  12. Macthetrix

    Macthetrix Regular Member

    Joined:
    Feb 21, 2013
    Messages:
    379
    Likes Received:
    390
    Occupation:
    The Boss
    Location:
    Silicon Valley
    export the fiddler file with the request and I will put it together for you :)
     
  13. gtothem

    gtothem Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 22, 2011
    Messages:
    236
    Likes Received:
    296
    Occupation:
    Coder
    Home Page:
  14. gtothem

    gtothem Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 22, 2011
    Messages:
    236
    Likes Received:
    296
    Occupation:
    Coder
    Home Page:
    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.
     
  15. spidertimo

    spidertimo Registered Member

    Joined:
    May 7, 2010
    Messages:
    90
    Likes Received:
    58
    Location:
    Germany
    Thanks but doesn't work for me :/ I changed it back and my source code looks like this:
    Code:
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create("DELETED/_mupload_/composer/?site_category=m_basic&waterfall_id=" & waterfallid & "&source_loc=composer_group"), HttpWebRequest)
    
            Dim tempCookies As New CookieContainer
            tempCookies = logincookie
    
    
            request.Method = "POST"
            request.KeepAlive = True
            request.CookieContainer = tempCookies
            request.ContentType = "multipart/form-data"
            request.Referer = "DELETED"
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
    
    
    
    
            Dim boundary2 As String = "-----------------------------" & DateTime.Now.Ticks.ToString & vbCrLf
            Dim boundary3 As String = "-----------------------------" & DateTime.Now.Ticks.ToString & "--"
            Dim BoundaryBytes() As Byte = System.Text.Encoding.Default.GetBytes(("Content-Type: multipart/form-data; boundary=" & boundary2 & vbCrLf))
    
    
            Dim InfoBytes() As Byte = System.Text.Encoding.Default.GetBytes("Content-Disposition: form-data; name=""fb_dtsg""" & vbCrLf & vbCrLf & dtsg3 & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""charset_test""" & vbCrLf & vbCrLf & "€,´,€,´,水,Д,Є" & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""file1""; filename=""249-414-366-421-3825184.jpg""" & vbCrLf & "Content-Type: image/jpeg" & vbCrLf)
    
    
            Dim FurtherBytes() As Byte = System.Text.Encoding.Default.GetBytes((vbCrLf & boundary2 & "Content-Disposition: form-data; name=""file2""; filename=""""" & vbCrLf & "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""file3""; filename=""""" & vbCrLf & "Content-Type: application/octet-stream" & vbCrLf & vbCrLf & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""caption""" & vbCrLf & vbCrLf & "Test" & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""return_uri""" & vbCrLf & vbCrLf & "/groups/645391745509569?view=group&fc=photo_upload_success" & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""return_uri_error""" & vbCrLf & vbCrLf & "DELETED/photos/upload/?target_id=645391745509569&upload_source=composer" & vbCrLf & boundary2 & vbCrLf & "Content-Disposition: form-data; name=""target""" & vbCrLf & vbCrLf & "645391745509569" & vbCrLf & boundary2 & "Content-Disposition: form-data;  name=""ref""" & vbCrLf & vbCrLf & "m_upload_pic" & vbCrLf & boundary2 & "Content-Disposition: form-data; name=""album_fbid""" & vbCrLf & vbCrLf & vbCrLf & boundary3))
    
    
            Dim FileInfo As New IO.FileInfo("C:\Pictures\2\249-414-366-421-3825184.jpg")
    
    
    
    
            request.ContentLength = BoundaryBytes.Length + InfoBytes.Length + FurtherBytes.Length + FileInfo.Length
    
    
            Dim RequestStream As IO.Stream = request.GetRequestStream
    
    
            RequestStream.Write(BoundaryBytes, 0, BoundaryBytes.Length)
            RequestStream.Write(InfoBytes, 0, InfoBytes.Length)
    
    
            Dim FileStream As IO.FileStream = IO.File.Open("C:\Pictures\2\249-414-366-421-3825184.jpg", _
                          IO.FileMode.Open)
    
    
            Dim Buffer(1024) As Byte
            Dim BytesRead As Integer
    
    
            Do
                BytesRead = FileStream.Read(Buffer, 0, Buffer.Length)
                RequestStream.Write(Buffer, 0, BytesRead)
            Loop Until BytesRead = 0
            RequestStream.Write(FurtherBytes, 0, FurtherBytes.Length)
    
    
    
    
            FileStream.Close()
            RequestStream.Close()
    
    
            Dim postresponse As HttpWebResponse
    
    
            postresponse = DirectCast(request.GetResponse(), HttpWebResponse)
    any ideas why it doesn't work? I had to delete the urls and replaced them with "DELETED". Because I can't post urls