Downloading Web Pages with the Winsock Control

The Professional and Enterprise Editions of Visual Basic are shipped with the Winsock control. This control allows you to use the low level Windows Sockets to connect to remote servers and execute commands or download Web pages. This tip shows how to connect to a server and download a Web page.

Code

You must have a Winsock control loaded into the project. This code assumes the control is called Winsock1. There also must be a text box on the form which is called txtWebPage.

First, connect to the remote computer using the following code:

Winsock1.RemoteHost = "jelsoft.com"
Winsock1.RemotePort = 80
Winsock1.Connect

In the Winsock1.Connect event, place the following code to download the web page.

Dim strCommand as String
Dim strWebPage as String
strWebPage = "http://www.jelsoft.com/index.html"
strCommand = "GET " + strWebPage + " HTTP/1.0" + vbCrLf 
strCommand = strCommand + "Accept: */*" + vbCrLf 
strCommand = strCommand + "Accept: text/html" + vbCrLf 
strCommand = strCommand + vbCrLf 
Winsock1.SendData strCommand

The Winsock control will attempt to download the page. When is receives some data from the server, it will trigger the DataArrival event.

Dim webData As String 
Winsock1.GetData webData, vbString
TxtWebPage.Text = TxtWebPage.Text + webData


Realtor Monmouth NJ | Mover Reliance | Shades | Small Business Merchant Account Solutions | Romance Toys