Microsoft & .NETVisual BasicPosting to CGI Scripts

Posting to CGI Scripts

It seems that almost every week I get a question that asks ‘how can I click the submit button through code?’. Well, the way to do it is to actually perform a post to the script that is behind the form. In this article I will teach you the basics of HTML forms, how to send data to a CGI script and finally how to open multiple sessions to different CGI scripts.

Lets start off with the basics. An HTML form is simply a nice looking form with a submit button, that, when pressed sends the data in the form to a script on the server that processes the data. A form can range from a simple feedback form (check out the one on VB Square) to VB Script enhanced, dynamic forms. For this article, I will only use a simple form with only a few fields.

If you know anything about HTML, then a form and its contents are found within a pair of <form> and </form> tags. To see these, right click on a form in your webbrowser and click view source. The most useful information for us when wanting to post to a script is looking at the method attribute in the <form> tag. A simple form looks something like this:

<form method=post action=/cgi-bin/feedback.cgi>
<input type=text name=email>
<input type=hidden name=recipient value=me@me.com>
<input type=submit name=submit value=Submit!>
</form>

The first line specifies that we will be posting the values to the feedback.cgi script. The next three lines are what you see on your web page, although the second line is hidden (as denoted by hidden :). This means that you won’t see this on your page, but it will be passed to the script.

Before we can post to a script, we need to know how they work. When writing a script in Perl, you get the values passed to the script using the following code:

forms_vars = ENV("QUERY_STRING")

The environment variables QUERY_STRING contains all the values passed to the script, whether as form input fields, or passed in the URL. This is good because we can then pass the form values in the url.

ASP, on the other hand, provides three ways of getting values passed to the script. The first, Request.Querystring() gets the all the stuff in the URL after the ?, but it ignores all the form values. Here is an example:

With this url: www.myserver.com/myscript.asp?name=sam the following code would give name=sam:

strQuery = Request.QueryString()

although this code would give nothing:

strQuery = Request.Form()

but this code would give name=sam

strQuery = Request()

So, if we want to post to an ASP script, then we need to make sure that it is using either Request.Querystring() or Request() to get the values, otherwise we can’t pass them on.

Anyway, stop blabbing! Lets get on with some code.

I suggest that you use the Internet Transfer Control to make the posts, although I suppose you could do it using the Winsock control. For more information on using the Internet Transfer Control check out my series online at:

But what happens when we want to get the results of the posting? Simply do the following:

Dim b() As Byte
Dim strData As String
Dim intCount As Integer

b() = Inet1.OpenURL("www.myserver.com/cgi-bin/feedback.cgi?email=me@me.com", icByteArray)

For intCount = 0 To UBound(b) - 1
    strData = strData & Chr(b(intCount))
Next

The variable strData now contains the data sent back from the script. You can now parse this variable to get any information that you want.

To post/get from several scripts at once, you can either have several Inet controls performing the same operation, or simply have a loop that posts the data using one Inet control. Either way, once you have mastered posting to one script, posting to several is pretty simple. Happy posting!

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories