By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Server always returns 404 errors on HTTP POST request containing binary data

P: 1
Hi

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

Expand|Select|Wrap|Line Numbers
  1. <html>    
  2. <title>HTTP Post Testing</title>    
  3. <body>    
  4. <form action=http://www.example.com/postdata    
  5. enctype="multipart/form-data" method="post" name="testform">    
  6. <input id="user_login" name="user[login]" size="30" type="hidden"    
  7. value="user01" />    
  8. <input id="user_password" name="user[password]" size="30"    
  9. type="hidden" value="123456" />    
  10.                <table>    
  11.                        <tr>    
  12.                                <td>File:</td>    
  13.  
  14.                                <td><input id="file" name="file"    
  15. type="file" /></td>    
  16.                        </tr>    
  17.                        <tr>    
  18.                                <td colspan="2"><input name="commit"    
  19. type="submit" value="Upload" name="upload"/></td>    
  20.                        </tr>    
  21.                </table>    
  22.        </form>    
  23. </body>    
  24. </html>   
  25.  
The target URL would return "OK" for successful upload and empty otherwise.

My code to generate the POST request is as followed:

Expand|Select|Wrap|Line Numbers
  1.         Try  
  2.             Dim request As HttpWebRequest = CType(WebRequest.Create(Me.txtURL.Text), HttpWebRequest) 'this textbox contains the target URL of the request   
  3.             ' Set the Method property of the request to POST.   
  4.             request.Method = "POST"  
  5.             request.Accept = "*/*"  
  6.             request.UserAgent = "curl/7.16.3"  
  7.             request.ProtocolVersion = HttpVersion.Version11   
  8.             request.Referer = "http://www.mydomain.com/"  
  9.             request.SendChunked = True  
  10.  
  11.             System.Net.ServicePointManager.Expect100Continue = False  
  12.  
  13.             ' Create POST data and convert it to a byte array.   
  14.             Dim postData As String = ""  
  15.  
  16.             postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""user[login]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)   
  17.             postData += "gape" + Chr(13) + Chr(10)   
  18.             postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""user[password]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)   
  19.             postData += "telipoint8" + Chr(13) + Chr(10)   
  20.             postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""file""; filename=""helpcurl.txt""" + Chr(13) + Chr(10)   
  21.             postData += "Content-Type: text/plain" + Chr(13) + Chr(10) + Chr(13) + Chr(10)   
  22.             postData += "fdfgfggfbfgggggggggggggggg" + Chr(13) + Chr(10) + Boundary + "--" + Chr(13) + Chr(10)   
  23.  
  24.             Dim encoding As New System.Text.ASCIIEncoding()   
  25.             Dim byteArray As Byte() = encoding.GetBytes(postData)   
  26.  
  27.             ' Set the ContentType property of the WebRequest.   
  28.             'request.ContentType = "application/x-www-form-urlencoded"   
  29.             request.ContentType = "multipart/form-data; boundary=" + Me.Boundary + vbCrLf + vbCrLf   
  30.             ' Set the ContentLength property of the WebRequest.   
  31.             request.ContentLength = byteArray.Length   
  32.  
  33.             ' Get the request stream.   
  34.             Dim dataStream As Stream = request.GetRequestStream()   
  35.             ' Write the data to the request stream.   
  36.             dataStream.Write(byteArray, 0, byteArray.Length)   
  37.             ' Close the Stream object.   
  38.             dataStream.Close()   
  39.  
  40.             ' Get the response.   
  41.             Dim response As WebResponse = request.GetResponse()   
  42.             ' Display the status.   
  43.             MsgBox(CType(response, HttpWebResponse).StatusDescription, MsgBoxStyle.Information, "Response Code")   
  44.  
  45.             ' Get the stream containing content returned by the server.   
  46.             dataStream = response.GetResponseStream()   
  47.  
  48.             ' Open the stream using a StreamReader for easy access.   
  49.             Dim reader As New StreamReader(dataStream)   
  50.             ' Read the content.   
  51.             Dim responseFromServer As String = reader.ReadToEnd()   
  52.  
  53.             ' Display the content.   
  54.             txtResponse.Text = responseFromServer 'this textbox shows the response from the server   
  55.  
  56.             ' Clean up the streams.   
  57.             reader.Close()   
  58.             dataStream.Close()   
  59.             response.Close()   
  60.         Catch Ex As Exception   
  61.             MsgBox(Ex.Message, MsgBoxStyle.Exclamation, "Error Encountered")   
  62.         End Try  
  63.  
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:

Expand|Select|Wrap|Line Numbers
  1. <%   
  2.  
  3. test1 = Request.Form("user[login]")   
  4. test2 = Request.Form("user[password]")   
  5.  
  6. Response.Write(test1 + "<br>" + test2)   
  7.  
  8. %>  
  9.  
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

Expand|Select|Wrap|Line Numbers
  1. curl -F user[login]=user01 -F user[password]=123456 -F
  2. file=@myfile.txt http://www.readysnap.com/print/mup and everything is exactly the same.
  3.  
Can anyone suggest what's wrong? Thanks. :)
Jul 22 '07 #1
Share this Question
Share on Google+
2 Replies


ClubbieTim
P: 2
Did you find a solution to this problem?...I've got a similar situation...
Jul 31 '07 #2

Plater
Expert 5K+
P: 7,872
Your postData string seems a little out-of-whack. Or at least rather confusingly built.
Have you verified (via a packet watcher perhaps?) that the data from your VB program is leaving the same way as when a regular html <form> sends it?

I just looked at a form I made and it sends this:
Expand|Select|Wrap|Line Numbers
  1. POST /ddd.cgi HTTP/1.1\r\n
  2. Accept: */*\r\nAccept-Language: en-us\r\n
  3. Content-Type: multipart/form-data; boundary=---------------------------7d71e911240248\r\n
  4. Accept-Encoding: gzip, deflate\r\n
  5. Host: g3_1:8080\r\n
  6. Content-Length: 490\r\n
  7. Connection: Keep-Alive\r\n
  8. Cache-Control: no-cache\r\n
  9. \r\n
  10. -----------------------------7d71e911240248\r\n
  11. Content-Disposition: form-data; name=\"fred\"\r\n
  12. \r\n
  13. bill\r\n
  14. -----------------------------7d71e911240248\r\n
  15. Content-Disposition: form-data; name=\"james\"\r\n
  16. \r\n
  17. Hi\r\n
  18. -----------------------------7d71e911240248\r\n
  19. Content-Disposition: form-data; name=\"somename\"; filename=\"\"\r\n
  20. Content-Type: application/octet-stream\r\n
  21. \r\n
  22. \r\n
  23. -----------------------------7d71e911240248\r\n
  24. Content-Disposition: form-data; name=\"sb\"\r\n
  25. \r\n
  26. CLICK\r\n
  27. -----------------------------7d71e911240248--\r\n
  28.  
based on the following html, with no file selected:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title></title>
  4. </head>
  5.  
  6. <body>
  7. <form method="post" action="http://g3_1:8080/ddd.cgi" enctype="multipart/form-data" >
  8.    <input name="fred" type="text" value="bill" />
  9.    <textarea name="james">Hi</textarea>
  10.    <input type="file" name="somename" > 
  11.    <input type="submit" name="sb" value="CLICK" />
  12. </form>
  13. </body>
  14. </html>
  15.  
Jul 31 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.