Ads Top

Use Winsock Control to download webpages

Overview:
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.

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.

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

Winsock1.RemoteHost = "microsoft.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.microsoft.com/index.shtml"
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

'end code
'coding by Phillip N

No comments:

Powered by Blogger.