Solved

POST Command with wininet

Posted on 2000-04-21
6
959 Views
Last Modified: 2013-11-13
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!
0
Comment
Question by:svlikhite
6 Comments
 
LVL 8

Accepted Solution

by:
stefanx earned 50 total points
ID: 2738744
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2738925
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
 
LVL 14

Expert Comment

by:mcrider
ID: 2739629
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:svlikhite
ID: 2745810
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
 
LVL 8

Expert Comment

by:stefanx
ID: 2746766
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
 
LVL 8

Expert Comment

by:stefanx
ID: 2746784
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to parse out a json string in VB6? 10 162
Looking for VB6 code to read SQL table export it to ascii 8 38
VBA Shell can't Find Word document 11 92
Problem to picture file 20 49
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now