Class: InternetHTTP in Category Internet/Web : HTTP/FTP from Total Visual SourceBook

Create Internet HTTP connections and sessions and retrieve HTML page sections and text. Includes support for VB6 and VBA 32 and 64-bit Windows API calls.

You can use the functionality in this class to query HTTP servers, request and send data, and manage HTTP sessions. This class works directly through the WININET.DLL library, so it requires no additional OCX or control files. This means you can distribute simple HTTP functionality with your application with little overhead. WININET.DLL is part of Internet Explorer.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CInternetHTTP class.
ConnectHandle Property Get the connection handle.
DLLVersionMajor Property Get the major version number of the WinInet.DLL library.
DLLVersionMinor Property Get the minor version number of the WinInet.DLL library.
HTTPRequestHandle Property Get the HTTPRequest handle.
OptionBufferLen Property Get the current Option Buffer Length. Used for HttpSendRequest (Function SendRequest).
OptionBufferString Property Get the current Option Buffer String. Used for HttpSendRequest (Function SendRequest).
ProxyName Property Get the current proxy name.
ProxyPassword Property Get the current proxy password.
SessionHandle Property Get the Session handle.
URL Property Get the current URL.
UserAgent Property Get the current user agent (a string that identifies the client to the HTTP server). This can any string, but is typically comprised of text identifying a specific browser.
UserName Property Get the current user name.
UserPassword Property Get the current user password.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Class_Terminate Terminate Close all connection handles.
CloseConnection Method Close the handle passed to it.
GetQueryInfo Method Read the information from the Server.
OpenConnection Method Open a connection to the provided URL. Must have opened a session prior to calling this function.
OpenHeaders Method Request the headers from the URL. Must have opened a Session, connection, and HTTPRequest handle prior to calling this function.
OpenHTTP Method Request the provided URL. Must have opened a session and connection prior to calling this function.
OpenSession Method Open a session handle to the WinINET dll. This must be done before any connections or retrieval parts of the class are called.
NOTE: "UserAgent" should be set already.
QueryInfo Method Query the information from the server, and return it to the caller.
ReadFile Method Read the information from the server, and return it to the caller.
SendRequest Method Send the request for information from the URL server.
SetOptionStrings Method Read proxy and user information from the server.
NOTE: Not designed to set both at the same time. Caller handles what error is returned from original QueryInfo request and uses this function appropriately.
SetTimeOuts Method Set time out lengths for connection time, receive time, and send time.
GetMainURL Private Request the main part of the provided URL.
GetUrlSubObject Private Request the sub directory or object of the provided URL.
GetHTML Method Get the HTML text of the current page.
' Example of CInternetHTTP
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_CInternetHTTP()
  ' Commments: Examples of using the CInternetHTTP class to create Internet HTTP connections and sessions and retrieve HTML page sections and text.
  '            Includes support for VB6 and VBA with 32 and 64 bit API calls.
  '            See results in the Immediate Window.

  Const cstrURL As String = ""
  Dim clsInternetHTTP As CInternetHTTP
  Dim fOK As Boolean
  Dim strHeader As String
  Dim lngTimeOut As Long
  Dim lngStatus As Long
  Dim lngStatusSize As Long
  Dim intCounter As Integer

  Set clsInternetHTTP = New CInternetHTTP
  clsInternetHTTP.UserAgent = "FMS SourceBook"

  ' Open a session connection through the Internet DLL
    Debug.Print "Version Major: " & clsInternetHTTP.DLLVersionMajor
    Debug.Print "Version Minor: " & clsInternetHTTP.DLLVersionMinor

    ' Set username and password.
    ' Make sure change to your user name and password if required.
    clsInternetHTTP.UserName = ""
    clsInternetHTTP.UserPassword = ""

    ' Set the URL to open and work with
    clsInternetHTTP.URL = cstrURL

    fOK = clsInternetHTTP.OpenConnection(INTERNET_DEFAULT_HTTP_PORT)

    ' For Secure SSL connections use INTERNET_DEFAULT_HTTPS_PORT:
    ' fOK = clsInternetHTTP.OpenConnection(INTERNET_DEFAULT_HTTPS_PORT)

    If fOK Then
      'Use "GET" to Get the connection values
      clsInternetHTTP.OptionBufferString = vbNullString
      clsInternetHTTP.OptionBufferLen = 0
      fOK = clsInternetHTTP.OpenHTTP("GET")

      'Use "POST" to post data to the server
      'clsInternetHTTP.OptionBufferString = clsInternetHTTP.URL
      'clsInternetHTTP.OptionBufferLen = Len(clsInternetHTTP.OptionBufferString)
      'fOK = clsInternetHTTP.OpenHTTP("POST")

      If fOK Then
        strHeader = "Accept-Language: en" & vbCrLf
        fOK = clsInternetHTTP.OpenHeaders(strHeader)

        strHeader = "Connection: Keep-Alive" & vbCrLf
        fOK = clsInternetHTTP.OpenHeaders(strHeader)

        strHeader = "Content-Type: application/x-www-form-urlencoded" & vbCrLf
        fOK = clsInternetHTTP.OpenHeaders(strHeader)

        lngTimeOut = 30000

        clsInternetHTTP.SetTimeOuts (lngTimeOut)

        'Because this could get stuck in an infinite loop, if bad information is passed, we want to only try 3 times
        For intCounter = 0 To 2
          fOK = clsInternetHTTP.SendRequest()

          lngStatusSize = Len(lngStatus)
          fOK = clsInternetHTTP.QueryInfo(HTTP_QUERY_FLAG_NUMBER Or HTTP_QUERY_STATUS_CODE, lngStatus, lngStatusSize)

          Select Case lngStatus
            Case 407  ' status.HTTP_STATUS_PROXY_AUTH_REQ
              ' Make sure change to your Proxy name and password.
              clsInternetHTTP.ProxyName = ""
              clsInternetHTTP.ProxyPassword = ""
              fOK = clsInternetHTTP.SetOptionStrings(True, False)

            Case 401  ' status.HTTP_STATUS_DENIED
              fOK = clsInternetHTTP.SetOptionStrings(False, True)

            Case Else
              ' No recognized error returned
              Exit For
          End Select

        If fOK Then
          ' Response headers
          Debug.Print "Content Type: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_CONTENT_TYPE)
          Debug.Print "Content Length: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_CONTENT_LENGTH)
          Debug.Print "Last Modified: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_LAST_MODIFIED)
          Debug.Print "Version: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_VERSION)
          Debug.Print "Status Code: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_STATUS_CODE)
          Debug.Print "Raw Headers: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_RAW_HEADERS)
          Debug.Print "Response Headers: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF)
          Debug.Print "Forwarded: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_FORWARDED)
          Debug.Print "Server: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_SERVER)
          Debug.Print "Request Method: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_REQUEST_METHOD)
          Debug.Print "Pragma: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_PRAGMA)
          Debug.Print "Request Headers: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_RAW_HEADERS_CRLF)
          Debug.Print "User Agent: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_USER_AGENT)
          Debug.Print "Request Method 2: " & clsInternetHTTP.GetQueryInfo(HTTP_QUERY_FLAG_REQUEST_HEADERS + HTTP_QUERY_REQUEST_METHOD)

          ' Get HTML from the specified URL
          Debug.Print "HTML from " & clsInternetHTTP.URL & ": " & vbCrLf & clsInternetHTTP.GetHTML()
          Debug.Print "HttpSendRequest failed with error: " & Err.LastDLLError
        End If
        Debug.Print "HttpOpenRequest failed with error: " & Err.LastDLLError
      End If
      Debug.Print "InternetConnect failed with error: " & Err.LastDLLError
    End If
    Debug.Print "InternetSession Open call failed with error: " & Err.LastDLLError
  End If

  ' Calls the Terminate Event of the class
  Set clsInternetHTTP = Nothing
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

View all FMS products for Microsoft Access All Our Microsoft Access Products


Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info




Free Product Catalog from FMS