This topic demonstrates how to write a Microsoft Visual Basic application that obtains the binary data from a Web site using
Microsoft Windows HTTP Services (WinHTTP).
Prerequisites and Requirements
In addition to a working knowledge of Visual Basic, this example requires the following:
- The
current version of the Platform Software Development Kit (SDK).
- The proxy configuration tool to establish the proxy settings for Microsoft Windows HTTP Services (WinHTTP), if your connection to the Internet is through a proxy server. See
ProxyCfg.exe, a Proxy Configuration Tool for more information.
- A familiarity with
network terminology and concepts.
Retrieving Data Synchronously
This section describes how to create a Visual Basic application that synchronously retrieves data over a network. It retrieves a binary image file and displays it in a user interface.
Begin by opening Visual Basic. Open a new Standard EXE project. The new project contains a form named "Form1".
The application uses some of the methods and properties of the
WinHttpRequest object. Use early binding to access this object by completing the following two steps.
- From the Project menu, click References.
- On the list of Available References, select Microsoft WinHttpRequest Component, version 5.0 and click OK.
If the
WinHttpRequest object is not available on the
Available References list, WinHttp.dll has not
been
properly installed or is not registered. When you use early binding with a
Component Object Model (COM) object, Visual Basic is aware of the methods and
properties for that object and can provide assistance with syntax checking.
It
is also able to call the methods and properties faster.
Create a user interface for the application by using the tools provided in Visual Basic. The form should include a
CommandButton named "Command1", a
TextBox named "Text1", and a
PictureBox named "Picture1". Arrange the controls on the form so that the form is similar to the following image.
.png)
Copy the following source code in the code area of the project:
Dim WinHttpReq As WinHttp.WinHttpRequest
Private Sub Form_Load()
' Create an instance of the WinHTTPRequest ActiveX object.
Set WinHttpReq = New WinHttpRequest
End Sub
Private Sub Command1_Click()
' Create an array to hold the response data.
Dim d() As Byte
' Assemble an HTTP Request.
WinHttpReq.Open "GET", _
"http://www.microsoft.com/library/homepage/images/ms-banner.gif",_
False
' Send the HTTP Request.
WinHttpReq.Send
' Put status and content type into status text box.
Text1.Text = WinHttpReq.Status & " - " & WinHttpReq.StatusText
' Put response data into a file.
Open "temp.gif" For Binary As #1
d() = WinHttpReq.ResponseBody
Put #1, 1, d()
Close
' Load the data file as a picture.
Picture1.Picture = LoadPicture("temp.gif")
End Sub
You now have a program that uses a
WinHttpRequest object to obtain the Ms-banner.gif file from the Web page at
www.microsoft.com. When you run the application and click the
CommandButton, the status code is displayed in the
TextBox and the resource is displayed as an image in the
PictureBox.
Analyzing the Code
The first line of the sample source code is a "Dim" statement, which creates a variable to store an instance of the
WinHttpRequest object. This is done at the module level so that the object is available to all the routines in this form module. The instance of the
WinHttpRequest object is created in the
Load event of the form:
'Create an instance of the WinHTTPRequest ActiveX object
Set WinHttpReq = New WinHttpRequest
When Visual Basic encounters this line, it creates an instance of this object. If you get the error value, "ActiveX component can't create object," the WinHttp.dll was not properly registered or is not present on the system.
The handler for the
Click event of the
CommandButton contains the rest of the source code. The "Dim"statement defines a data array to temporarily hold the data from
ResponseBody. Next, the
Open method of the
WinHttpRequest object assembles an HTTP request:
' Assemble an HTTP Request.
WinHttpReq.Open "GET", _
"http://www.microsoft.com/library/homepage/images/ms-banner.gif", _
False
Three parameters specify which HTTP verb to use, the name of the resource, and whether to use WinHTTP synchronously or asynchronously. In this example, the method is using the HTTP verb "GET" to obtain data from http://www.microsoft.com. Specifying FALSE for the last parameter determines that the transaction occurs synchronously. The
Open method does not establish a connection to the resource as the name might imply. Rather, it initializes the internal data structures that maintain information about the session, connection, and request.
The
Send method assembles the request headers and sends the request. When called in synchronous mode, the
Send method also waits for a response before allowing the application to continue:
' Send the HTTP request.
WinHttpReq.Send
Execution of the script pauses while the entire body of the resource is retrieved. After the
Send method, the program accesses the status code and status text and displays them in the
TextBox:
'Put status and content type into status text box.
Text1.Text = WinHttpReq.Status & " - " & WinHttpReq.StatusText
Since binary data cannot be loaded directly into a
PictureBox control, the program opens a temporary file to hold the data:
' Put response data into a file.
Open "temp.gif" For Binary As #1
The program then obtains the response by accessing the
ResponseBody property of the
WinHttpRequest object. Since
ResponseBody returns an array of unsigned bytes, the data must be received by a byte array:
d() = WinHttpReq.ResponseBody
The application places the data returned by the
ResponseBody property into the data array, "d()", where it is written to the temporary file using the
Put statement. The file is then closed. The picture is then loaded from the temporary file into the
PictureBox control, where it is displayed to the user:
'Load temp.gif to Picture1.
Picture1.Picture = LoadPicture("temp.gif")
The
WinHttpRequest object ensures that any internal resources that are allocated for the HTTP transaction are released.
See Also
- HTTP/1.1
Request for Comments
(RFC)
- WinHttpRequest
Send comments about this topic to Microsoft
Build date: 3/12/2009