HTTP POST with binary data in .NET

3 posts, 0 answers
  1. mdanh2002
    mdanh2002 avatar
    80 posts
    Member since:
    May 2007

    Posted 21 Jul 2007 Link to this post


    From VB.NET I want to simulate the POST request of the following HTML form

    <title>HTTP Post Testing</title>   
    <form action=   
    enctype="multipart/form-data" method="post" name="testform">   
    <input id="user_login" name="user[login]" size="30" type="hidden"   
    value="user01" />   
    <input id="user_password" name="user[password]" size="30"   
    type="hidden" value="123456" />   
                                   <td><input id="file" name="file"   
    type="file" /></td>   
                                   <td colspan="2"><input name="commit"   
    type="submit" value="Upload" name="upload"/></td>   

    The target URL would return "OK" for successful upload and empty otherwise.

    My code to generate the POST request is as followed:

                Dim request As HttpWebRequest = CType(WebRequest.Create(Me.txtURL.Text), HttpWebRequest) 'this textbox contains the target URL of the request  
                ' Set the Method property of the request to POST.  
                request.Method = "POST" 
                request.Accept = "*/*" 
                request.UserAgent = "curl/7.16.3" 
                request.ProtocolVersion = HttpVersion.Version11  
                request.Referer = "" 
                request.SendChunked = True 
                System.Net.ServicePointManager.Expect100Continue = False 
                ' Create POST data and convert it to a byte array.  
                Dim postData As String = "" 
                postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""user[login]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)  
                postData += "gape" + Chr(13) + Chr(10)  
                postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""user[password]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)  
                postData += "telipoint8" + Chr(13) + Chr(10)  
                postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""file""; filename=""helpcurl.txt""" + Chr(13) + Chr(10)  
                postData += "Content-Type: text/plain" + Chr(13) + Chr(10) + Chr(13) + Chr(10)  
                postData += "fdfgfggfbfgggggggggggggggg" + Chr(13) + Chr(10) + Boundary + "--" + Chr(13) + Chr(10)  
                Dim encoding As New System.Text.ASCIIEncoding()  
                Dim byteArray As Byte() = encoding.GetBytes(postData)  
                ' Set the ContentType property of the WebRequest.  
                'request.ContentType = "application/x-www-form-urlencoded"  
                request.ContentType = "multipart/form-data; boundary=" + Me.Boundary + vbCrLf + vbCrLf  
                ' Set the ContentLength property of the WebRequest.  
                request.ContentLength = byteArray.Length  
                ' Get the request stream.  
                Dim dataStream As Stream = request.GetRequestStream()  
                ' Write the data to the request stream.  
                dataStream.Write(byteArray, 0, byteArray.Length)  
                ' Close the Stream object.  
                ' Get the response.  
                Dim response As WebResponse = request.GetResponse()  
                ' Display the status.  
                MsgBox(CType(response, HttpWebResponse).StatusDescription, MsgBoxStyle.Information, "Response Code")  
                ' Get the stream containing content returned by the server.  
                dataStream = response.GetResponseStream()  
                ' Open the stream using a StreamReader for easy access.  
                Dim reader As New StreamReader(dataStream)  
                ' Read the content.  
                Dim responseFromServer As String = reader.ReadToEnd()  
                ' Display the content.  
                txtResponse.Text = responseFromServer 'this textbox shows the response from the server  
                ' Clean up the streams.  
            Catch Ex As Exception  
                MsgBox(Ex.Message, MsgBoxStyle.Exclamation, "Error Encountered")  
            End Try 

    Here I tried to simulate the upload of a text file. When I tried this code the server alays return 404 (Resource not Found) even though the URL is correct. When the ContentType is changed to "application/x-www-form-urlencoded", i.e. no file upload but only transfer normal text fields, the server seems to accept the request and returns empty (unsucessful upload), which is correct. When I tried the above code (with content-type=multipart-formdata) against an ASP script put on my locahost:

    test1 = Request.Form("user[login]")  
    test2 = Request.Form("user[password]")  
    Response.Write(test1 + "<br>" + test2)  

    my script returns empty strings, which means the POST request generated by my VB code is malformed. But I cannot see what's wrong. I have compared it to the output of the following curl command

    curl -F user[login]=user01 -F user[password]=123456 -F
    file=@myfile.txt and everything is exactly the same.

    Can anyone suggest what's wrong? Thanks. :)
  2. Peter Double
    Peter Double avatar
    1 posts
    Member since:
    Jan 2006

    Posted 22 Oct 2007 Link to this post

    Why is this question all over the internet but not answered anywhere???

    Did anyone every find the answer to this question?

    I am having similar problems with creating a curl in .Net
  3. Alex
    Alex avatar
    1 posts
    Member since:
    Apr 2009

    Posted 16 Apr 2009 Link to this post

    This is not just an issue with VB, I have the same problem when trying to transfer binary data with the HTTP POST method to a PHP page.

    It is not just the fact that the data are not transmitted, but the whole variable seems to have disappeared completely at the receiving end. The isset($_POST["password"]) gives false, which means that not only the value of the password is lost from the request but the entire password variable itself.

    If anyone knows a work-around for this, please let us know!
Back to Top