Solved

POST Command with wininet

Posted on 2000-04-21
6
952 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:svlikhite
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 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

9 Experts available now in Live!

Get 1:1 Help Now