Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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 : works fine if I create the form he suggests.

Any ideas how I can do it without the form?

Bill Kennedy.
Question by:wrk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

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:


Expert Comment

ID: 7145325

The class I wrote to handle URL encoding / decoding can be found at
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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 & "/ 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

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

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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