473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 6260
Did you find a solution to this problem?...I've got a similar situation...
Jul 31 '07 #2
Plater
7,872 Expert 4TB
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

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

Similar topics

12
by: lawrence | last post by:
How do I get PHP to tell the server that when I echo text to the screen, I need for the text to be sent as UTF-8? How does Apache know the right encoding when all the text is being generated by...
19
by: Thue Tuxen Sørensen | last post by:
Hi everybody ! I´m maintaining a large intranet (approx 10000 concurrent users) running on one IIS box and one DB box with sqlserver 2000. Currently there is 2,5 GB Ram, 1 1400 mhz cpu and 2...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
10
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post">...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
7
by: Sirplaya | last post by:
I am retrieving images that I stored in SQL Server on my web pages in C#. I have no problem with the images displaying, however, I am trying to wrap the image with an <A HREF ..." and each time I...
8
by: BiT | last post by:
Hello, I'm working right now on project in vb.net 2005 for my company, i need the project to download file from the company web site. In order to get the file i have to give the site address...
2
by: kodart | last post by:
Introduction Performance is the main concern to most server application developers. That’s why many of them anticipate using .NET platform to develop high performance server application regardless...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.