Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


VB6 HowTo POST request using WinInit APIs

Posted on 2007-07-30
Medium Priority
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

ID: 19594023
See http://support.microsoft.com/kb/259100/ ("SAMPLE: Vbhttp.exe Demonstrates How to Use HTTP WinInet APIs in Visual Basic")
LVL 38

Accepted Solution

PaulHews earned 2000 total points
ID: 19594033
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 HTTP_ADDREQ_FLAG_ADD = &H20000000

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.
        '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


Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

580 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