473,581 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 "applicatio n/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 6293
ClubbieTim
2 New Member
Did you find a solution to this problem?...I've got a similar situation...
Jul 31 '07 #2
Plater
7,872 Recognized Expert Expert
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
3736
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 PHP? If I build a content management system (I have) and I make sure that all input is encoded as UTF-8, how will the server know that the text in the...
19
14829
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 scsi disks installed on the db box. Sqlserver is set to use max 1,4 GB RAM, and the sqlserver does not seem to be using it all.
4
6451
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 define the servername / hostname in my Perl Progrem.. Here is the code:
10
1926
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"> <input type="hidden" name="thisPage" value="pispCheckDomain"> <input type="hidden" name="username" value="test"> <input type="hidden"...
2
4910
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 being updated by some other process through remoting. When the page loads, I init the tree, and in my browser I can see the initialized tree. The...
7
2052
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 try, it acts like the link is not even on the image. What is the proper way to do this? Thanks.
8
2986
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 http://www.somthing.com and then send to the server post data like "&d=10202" to get the file. i've search google and newsgroups but I didn't find...
2
18338
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 of the security features it provides. Microsoft Windows provides a high performance model that uses I/O completion port (IOCP) to process network...
0
7882
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.