Go Premium for a chance to win a PS4. Enter to Win


File uploads via HTTP using Winsock but not forms

Posted on 2002-07-09
Medium Priority
Last Modified: 2013-11-13

I'm developing an application which will transfer binary files between a VB client and the web.  It works at the moment courtesy of me having developed a basic FTP client which communicates with an FTP server (via a Winsock control) and transfers files using RETR and STOR commands.  The application also communicates with a Perl interface to MySQL via HTTP.

I can't help feeling that life would be simpler if I handled the file transfers using HTTP.  FTP requires a log in, changing directories, PORT commands, keeping the session alive and more (all of which may or may not succeed and as such have to be handled when they don't) whereas HTTP seems far simpler - GET and PUT/POST - which I figure would either work or not.

GETting a file is easy.  I wouldn't be typing this question if that's all I wanted to do.  I also want to PUT files, but I understand that servers may be configured to not allow this.  So what are my options?

The POST command looks like a suitable candidate, but sadly my knowledge of HTTP/CGI/Perl is not sufficient for me to know HOW I could use it.  My thoughts are to build a request on the client which consists of the appropriate headers and a long stream of binary data (potentially several megabytes worth) which constitute the file.  The server-side script would then turn that back into a file.

I've seen Perl which claims to do this, but involves the use of a form at the client end.  Remember that I'm trying to do this by building a request in a VB client and sending it through a Winsock control.  abel's code here, for example : http://www.experts-exchange.com/questions/Q_10292336.html works fine if I create the form he suggests.

Any ideas how I can do it without the form?

Bill Kennedy.
Question by:wrk
  • 4
  • 3

Expert Comment

by:Alon Hirsch
ID: 7142589

I've accomplished a similar thing, but from a VB front-end.
The biggest problem, was trying to understand what the HTTP message looked like.
In order to decipher this, I created a VB program that would listen on port 81 and simply display in a text box all data that arrived on that port.
I then created an HTML form that would allow an upload of a file to the server and set the Form action in the HTML to post to port 81 (my IP listener).
When I submitted the data from the HTML - I could see what needed to be sent from my VB program.
From then on it was simply a case of reading the stream of data from the file into the HTTP stream and sending it to the server.

The company I am contracting to has also just completed this in an OCX which is placed on a web page.

The biggest challenge there is the HTTP protocol.
If you want I can email you VB6 DLL (no source unfortunately) that will allow the transfer you require.
You can then reference the DLL from your project and perform the neccessary....

One other thing you need to look out for ... URLEncoding.
Data being sent to a web site needs to be URL Encoded before being sent.
I can include the code for URL Encoding if you require as well.


Author Comment

ID: 7142891

Thanks for your response.

I too tried sniffing the data being sent in order to build a message which would fool the script into thinking it had been submitted by a form, but I couldn't make that work.

The sniffer on the server would suggest that my request is correctly formatted - it *looks* the same as one which comes from a submitted form, yet the Perl script seems to ignore it.

It sounds like I could be falling foul of the encoding you mention, so if could send me some code that would be greatly appreciated.

My email address:  wrk@star-light.fsnet.co.uk


Expert Comment

by:Alon Hirsch
ID: 7145325

The class I wrote to handle URL encoding / decoding can be found at http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=31731&lngWId=1
It is available as freeware.

Another thing to check - in order for the Server to recognise Form data being submitted, you need the following in the HTTP Header :
Content-Type: application/x-www-form-urlencoded

Without it - the form data is simply ignored.

If you're still having problems, email me the source code and I'll try it with IIS - maybe it's a funny on Perl ?

My email address is alonh@debtpack.com

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 7179613
OK, I've figured this out.  The problem I was having was related to the boundary information.  This seems to be an area which isn't particularly well documented on the web (unless I have just been very unlucky when searching), but which is crucial to this working.

The following code seems to do the trick :

HTTPBoundary = <some random string of characters which won't occur anywhere else>

PreBinaryStructure = "--" & HTTPBoundary & vbCrLf & "Content-Disposition: form-data; name=""filename""; filename=""" & RemoteFileName & """" & vbCrLf & "Content-Type: application/octet-stream" & vbCrLf & vbCrLf

PostBinaryStructure = vbCrLf & "--" & HTTPBoundary & "--"

TotalLength = Len(PreBinaryStructure) + FileLength + Len(PostBinaryStructure)
Request = "POST http://" & Host & CGIBINDirectory & "/UploadScript.pl HTTP/1.0" & vbCrLf & "Accept: */*" & vbCrLf & "Content-Type: multipart/form-data; boundary=" & HTTPBoundary & vbCrLf & "Content-Length: " & Format$(TotalLength) & vbCrLf & "Connection: Close" & vbCrLf & vbCrLf

To upload the file to a Perl script such as abel's (see my original post), you need to send

The Binary data itself

in that order.  And the minus signs which precede (and follow) the boundary string are VITAL, and that was where my error was.  The vbCrLfs are pretty important too.

Hope this helps somebody.


Expert Comment

by:Alon Hirsch
ID: 7179634
Hi Bill,

I tried to email you several times - including the differences between the HTTP posts from your code and mine.
All email came back undelivered after a week.

Sorry I couldn't be of more help.


Author Comment

ID: 7179642
Strange, because I've been drowning in email as usual, so there are no obvious problems there.

Thanks for your help anyway - I'll email you a beer !!


Expert Comment

by:Alon Hirsch
ID: 7179686
Cheers ;-)

Accepted Solution

Mindphaser earned 800 total points
ID: 7180179
wrk provided his own solution, therfore I will refund his points.


To award extra points to AlonHirsch, please post a 50p question in this Topic Area with the title "Points for AlonHirsch" and a body "For your Help in <URL>".

** Mindphaser - Community Support Moderator **

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question