POST Command with wininet

How can I use POST command with wininet dll in VB? Currently I am using XMLHttpRequest object which comes with msxml.dll version 2.0. But this is does not work with IE4. So I am looking for solution with wininet dll.  

Thanks in advance!
svlikhiteAsked:
Who is Participating?
 
stefanxConnect With a Mentor Commented:
Maybe someone else has figured it out, but I just could not get the WinInet control to post properly. This was VB5 so maybe it's fixed in VB6, or maybe I'm just too stupid to get it to work.
So, I've written HTTP POST using the Winsock control.

If you can live with this, I'll be happy to give you the code.
0
 
AzraSoundCommented:
api function called httpsendrequest

Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, sOptional As Any, ByVal lOptionalLength As Long) As Long


see msdn for list of all internet related api calls:

http://msdn.microsoft.com/workshop/networking/wininet/reference/functions/all_functions.asp

however you will notice it is all referenced in c/c++

so check out this project which incorporates many of the functions including the one I mentioned above.

http://www.vbip.com/wininet/wininet_webchecker.asp

0
 
mcriderCommented:
See the following microsoft KB Article:

FIX: Internet Transfer Control 5.0 "POST" Request Doesn't Work
http://support.microsoft.com/support/kb/articles/Q167/7/06.asp?LN=EN-US&SD=msdn&FR=0 


Cheers!®©
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
svlikhiteAuthor Commented:
Thanks for the reply. How can I get the code which you have mentioned. I would be really happy to use that code. Thanks.
0
 
stefanxCommented:
OK, here is a routine that you can use for posting to a webserver. For it to work, you embed the routine in your form. You also need two controls on your form. These are :

(1) A Winsock control named Winsock and having an index property of 0.

(2)A timer control named TimerWinsock. Set its Enabled property to false.

as well as one global variable to be able to receive message from your winsock control. This variable is named WinsockRec$ and you can define it in either a module or in your form as :

Public WinsockRec$

The PostResult subroutine below takes 4 parameters as follows :

Host$ - A string variable representing the host that you want to connect to. It can be either a FQDN for example www.yahoo.com or an IP address like 196.15.11.22

Q$ - The Data that you wish to post. This normally contains your form data and to illustrate it, let's take and example. Say you have a form as follows:

<FORM>
<INPUT TYPE=HIDDEN NAME="X" VALUE="1234">
<INPUT SIZE=10 NAME="Z" VALUE="Hello">
<TEXTAREA NAME="Y" ROWS=5 COLS=10>Type it here</TEXTAREA>
</FORM>

Then Q$ should look as follows :
X=1234&Z=Hello&Y=Type%20it%20here

Doc: The name of the script that you want to post to. For example, if you want to post to http://www.acme.com/cgi-bin/doit.sh, then Doc would be set to "/cgi-bin/doit.sh"

Result - This is a parameter that is returned to your function. It contains the response of the remote script once you have posted your data. You will see that the result is the raw information that the webserver passes back, not just HTML.

The function returns a boolean value that indicates that the post was successful (true) or unsuccessful (false).

'---------------------------------
Private Function PostResult(ByVal Host$, ByVal Q$, ByVal Doc As String, ByRef Result As String) As Boolean

  ' This function uses the embedded Winsock control to
  ' send POST type data to a document. The Data to post is passed
  ' in Q$, the document to post to in Doc and the result in Result.
  ' The function returns a boolean to indicate success or failure.
 
  On Local Error Resume Next
 
  Dim Z$
 
  If Doc = "" Then
    PostResult = False
    Exit Function
  End If
  Load Winsock(1)
  Err = 0
  Winsock(1).RemoteHost = Host$
  Winsock(1).RemotePort = 80
  TimerWinsock.Interval = TimeoutValue
  TimerWinsock.Enabled = True
  Winsock(1).Connect
  While TimerWinsock.Enabled And (Winsock(1).State <> sckConnected)
    DoEvents
  Wend
  TimerWinsock.Enabled = False
  If Winsock(1).State = sckConnected Then
    Z$ = "POST " & "http://" & Host$ & Doc & " HTTP/1.0" & vbCrLf
    Z$ = Z$ & "Content-Type: application/x-www-form-urlencoded" & vbCrLf
    Z$ = Z$ & "Content-Length: " & Format$(Len(Q$)) & vbCrLf
    Z$ = Z$ & "Connection: Keep-Alive" & vbCrLf & vbCrLf
    Z$ = Z$ & Q$ & vbCrLf
    TimerWinsock.Interval = TimeoutValue
    WinsockRec$ = ""
    TimerWinsock.Enabled = True
    Winsock(1).SendData Z$
    While (Winsock(1).State <> sckClosing) And TimerWinsock.Enabled
      DoEvents
    Wend
    TimerWinsock.Enabled = False
    If Winsock(1).State = sckClosing Then
      Winsock(1).Close
      Result = WinsockRec$
      Unload Winsock(1)
      PostResult = True
      Exit Function
    End If
    Winsock(1).Close
    Unload Winsock(1)
    PostResult = False
  Else
    Winsock(1).Close
    Unload Winsock(1)
    PostResult = False
  End If

End Function
'-------------------------------

You further need two snippets of event handling code. The first of these is on the TimerWinsock_Timer() event which is simply as follows :

'-------------------------------
Private Sub TimerWinsock_Timer()
  TimerWinsock.Enabled = False
End Sub
'-------------------------------

and on your Winsock_DataArrival(Index as Integer) event as follows :

'------------------------------------
Private Sub Winsock_DataArrival(Index AS Integer, ByVal bytesTotal AS Long)

  On Local Error Resume Next

  Dim Q$

  Err = 0
  While (Winsock(Index).BytesReceived > 0) AND (Err=0)
     Winsock(Index).GetData Q$,vbString,bytesTotal
     WinsockRec$ = WinsockRec$ & Q
  WEnd
End Sub
'-------------------------------------

Please note that the PostResult Function above is not designed to be called asynchronously. This is mostly because of the fixed index used when loading a new socket. It means that you should not try and call more than one of the routines simultaneously (not that you should be able to anyway). Hope that helps you. If I made any typos, please excuse them.
0
 
stefanxCommented:
I forgot to add, you need another constant or variable called TimeOutValue that effectively specifies how long the routine should wait for a response to the POST request. The value to use totally depends on how far (network-wise) the webserver is from you. I would recommend a value like 30000 (30 seconds) to start off with. Best is probably to define this in your form or a module as :

Public Const TimeOutValue = 30000
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.