?
Solved

POST Command with wininet

Posted on 2000-04-21
6
Medium Priority
?
984 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
[X]
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
6 Comments
 
LVL 8

Accepted Solution

by:
stefanx earned 150 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

719 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