VB6 HowTo POST request using WinInit APIs

How would I do a Post request using the WinInit APIs in VB6? I see from MS msdn that its possible but I'm not great at APIs, buffers and such.

I'm using the VB6 sample here for WinInit APIs
Return the Contents of a Web Page Using WinInet API
http://www.freevbcode.com/ShowCode.Asp?ID=1252

My project is discussed here:
http://www.experts-exchange.com/Software/Internet_Email/Web_Browsers/Internet_Explorer/Q_22718901.html
dnotestineAsked:
Who is Participating?
 
PaulHewsCommented:
Usage:
Option Explicit

Private Sub Command1_Click()
    Debug.Print PostInfo("www.domain.com", "/folder/test.asp", "fname=John&lname=Smith")
End Sub



Option Explicit

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszCallerName As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal lpszServerName As String, ByVal nProxyPort As Integer, ByVal lpszUsername As String, ByVal lpszPassword As String, ByVal dwService As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" (ByVal hInternetSession As Long, ByVal lpszVerb As String, ByVal lpszObjectName As String, ByVal lpszVersion As String, ByVal lpszReferer As String, ByVal lpszAcceptTypes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal sOptional As String, ByVal lOptionalLength As Long) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInternetHandle As Long) As Boolean
Private Declare Function HttpAddRequestHeaders Lib "wininet.dll" Alias "HttpAddRequestHeadersA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lModifiers As Long) As Integer

Const INTERNET_FLAG_RELOAD = &H80000000
Const HTTP_ADDREQ_FLAG_ADD = &H20000000
Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000


Public Function PostInfo(Server As String, Script As String, PostData As String) As String

    Dim hInternetOpen As Long
    Dim hInternetConnect As Long
    Dim hHttpOpenRequest As Long
    Dim bRet As Boolean
    Dim bDoLoop  As Boolean
    Dim sReadBuffer As String * 4096
    Dim lNumberOfBytesRead  As Long
    Dim sBuffer As String
    Dim sHeader As String
    Dim lPostDataLen As Long
 
    hInternetOpen = 0
    hInternetConnect = 0
    hHttpOpenRequest = 0
   
    'Use registry access settings.
    Const INTERNET_OPEN_TYPE_PRECONFIG = 0
   
    hInternetOpen = InternetOpen("MyBrowser", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
   
    If hInternetOpen <> 0 Then
        'Type of service to access.
        Const INTERNET_SERVICE_HTTP = 3
        Const INTERNET_DEFAULT_HTTP_PORT = 80
        'Change the server to your server name
        hInternetConnect = InternetConnect(hInternetOpen, Server, INTERNET_DEFAULT_HTTP_PORT, vbNullString, "HTTP/1.0", INTERNET_SERVICE_HTTP, 0, 0)
   
        If hInternetConnect <> 0 Then
            'Brings the data across the wire even if it locally cached.
           
            hHttpOpenRequest = HttpOpenRequest(hInternetConnect, "POST", Script, "HTTP/1.1", vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
            If hHttpOpenRequest <> 0 Then
                sHeader = "Content-Type: application/x-www-form-urlencoded"
                lPostDataLen = Len(PostData)
                bRet = HttpSendRequest(hHttpOpenRequest, sHeader, Len(sHeader), PostData, lPostDataLen)
               
                bDoLoop = True
                Do While bDoLoop
                    sReadBuffer = vbNullString
                    bDoLoop = InternetReadFile(hHttpOpenRequest, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
                    sBuffer = sBuffer & Left(sReadBuffer, lNumberOfBytesRead)
                    If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
                Loop
               
                PostInfo = sBuffer
                bRet = InternetCloseHandle(hHttpOpenRequest)
                End If
                bRet = InternetCloseHandle(hInternetConnect)
            End If
            bRet = InternetCloseHandle(hInternetOpen)
       End If
  End Function

0
 
jkrCommented:
See http://support.microsoft.com/kb/259100/ ("SAMPLE: Vbhttp.exe Demonstrates How to Use HTTP WinInet APIs in Visual Basic")
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.