Aug
1
2003
VB6 // Wininet // HTTP

HTTP Class

Implement HTTP POST and GET with the HHTP Class using the Wininet Library

This class simplifies using the Wininet Library to implement HTTP requests. Using this class it's a piece of cake to POST Url-Encoded form data to a server.

This code has been revised (28 Aug 2003): There was an error in the URLEncode function where a hexed value was not necessarily 2 digits long. The SendRequest function has also been updated to download the request directly from the server by default (Reload).

This code has been revised (02 Sep 2003): The While loop for reading the returned data in SendRequest had faulty logic (changed to While r and (Read <> 0))

This code has been revised (29 Nov 2003): Updated OpenHTTP to both add the option to connect to any port and the ability to authenticate using basic authentication (plain text).

What it can't do

  • Return Progress Messages
  • Return Detailed Error Messages
  • Return the Response Header

Usage

Add HTTPClass.cls to your project

   'Example: POST a Form

   Dim h As HTTPClass

   Set h = New HTTPClass

   h.Fields("Username") = "Andrew"
   h.Fields("Email") = "andrew@paradoxes.info"
   h.Fields("Password") = "Secret"

   If h.OpenHTTP("www.paradoxes.info") Then
      Debug.Print h.SendRequest("test.asp", "POST")
   End If

   Set h = Nothing

   'Example: Download an Image to file

   Dim fh As Long
   Dim h As HTTPClass

   Set h = New HTTPClass

   If h.OpenHTTP("www.paradoxes.info") Then
      fh = FreeFile
      Open App.Path & "\vbcode.jpg" For Binary As #fh
      Put #fh, , h.SendRequest("/pics/vbcode.jpg", "GET")
      Close #fh
   End If

   Set h = Nothing

Downloads

  HTTPClass.zip - contains: HTTPClass.cls (1.9 kb)

More...