POSTing Form Data to a Web Page

WinINet API Functions

Using the WinINet API functions to post form data may seem like overkill. Especially when the ServerXMLHTTP object exists. However, if you need to post the contents of a file (emulating the <INPUT type=file> tag) or inform the user of the progress of a download, then I've found that the WinINet functions are easier to use. And the level of control that is available far surpasses the ServerXMLHTTP object. As I've already mentioned, the best tool depends on your goals.

Establishing a connection using the WinINet functions is a multi-step process. First the InternetOpen function is called to initialize the remaining functions. Then the InternetConnect function is called to identify the type of service being requested. This is necessary as the same set of functions can be used to interact with a number of different servers (including FTP, Gopher and Archie).

' Open the connection

Dim hInternetOpen As Long
Dim hInternetConnect As Long

hInternetOpen = InternetOpen("http generic", _
    vbNullString, _
    vbNullString, _

' Identify the type of service that is being accessed
hInternetConnect = InternetConnect(hInternetOpen, _
    "tagdataserver", _
    vbNullString, _
    "HTTP/1.0", _
    0, _

In the code sample for this article, I've minimized the error checking in the name of saving space. For example, the two functions shown above both return Windows handles. What this means to our error checking is that if the returned values are 0, then an error has occurred and we would need to act accordingly.
Once we've identified the service, we need to indicate the page to which the data will be posted. In this example, it will be TestPost.asp.

hHttpOpenRequest = HttpOpenRequest(hInternetConnect, _
    "POST", _
    "/TestPost.asp", _
    "HTTP/1.0", _
    vbNullString, _
    0, _

Now comes the trick. Putting the data into the request. As with the ServerXMLHTTP object, we need to add a header to notify the web server that the incoming data is form encoded and then send the request.

Dim sHeader As String
Dim bResult As Boolean

sHeader = "Content-Type: application/x-www-form-urlencoded" & vbCrLf
bResult = HttpAddRequestHeaders(hHttpOpenRequest, _
    sHeader, Len(sHeader), HTTP_ADDREQ_FLAG_REPLACE _

Dim lpszPostData As String
Dim lPostDataLen As Long

lpszPostData = "Id=1&S=2"
lPostDataLen = Len(lpszPostData)
bResult = HttpSendRequest(hHttpOpenRequest, _
    vbNullString, _
    0, _
    lpszPostData, _

Finally, we need to collect our reward for all this effort...not to mention the data that is returned from the web page.

Dim sBuffer As String
Dim sReadBuffer As String * 2048
Dim bDoLoop as Boolean
Dim lNumberOfBytesRead as Long

bDoLoop = True
While bDoLoop
    sReadBuffer = vbNullString
    bDoLoop = InternetReadFile(hHttpOpenRequest, _
    sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
    sBuffer = sBuffer & _
        Left(sReadBuffer, lNumberOfBytesRead)
    If Not CBool(lNumberOfBytesRead) Then
        bDoLoop = False
    End If

At this point, the variable sBuffer contains the raw HTML that was returned from the post. And like the good little developers that we are, we always clean up after ourselves.

bResult = InternetCloseHandle(hHttpOpenRequest)
bResult = InternetCloseHandle(hInternetConnect)
bResult = InternetCloseHandle(hInternetOpen)

You might also like...


About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”