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
Solved

POST Command with wininet

Posted on 2000-04-21
6
963 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 109
Hide vba in gp 7 104
How to measure sizes and angles in scanned images ? 3 76
VBA: loop recent folder and copy txt file. 8 34
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

828 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