VB6 HowTo POST request using WinInit APIs

Posted on 2007-07-30
Last Modified: 2013-12-20
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

My project is discussed here:
Question by:dnotestine
    LVL 86

    Expert Comment

    See ("SAMPLE: Vbhttp.exe Demonstrates How to Use HTTP WinInet APIs in Visual Basic")
    LVL 38

    Accepted Solution

    Option Explicit

    Private Sub Command1_Click()
        Debug.Print PostInfo("", "/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.
        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
                    PostInfo = sBuffer
                    bRet = InternetCloseHandle(hHttpOpenRequest)
                    End If
                    bRet = InternetCloseHandle(hInternetConnect)
                End If
                bRet = InternetCloseHandle(hInternetOpen)
           End If
      End Function


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    779 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

    21 Experts available now in Live!

    Get 1:1 Help Now