Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Download HTML Web Page
VB6 socket component example code showing how to download a simple HTML web page from a Web server. Dim WithEvents socket As ChilkatSocket Dim bAbort As Boolean Private Sub AbortButton_Click() ' Cause the socket operation to be aborted in the next heartbeat callback. bAbort = True End Sub ' The heartbeat event callback is called during blocking socket operations. ' If abort is set to True, the socket operation is aborted. Private Sub socket_Heartbeat(ByVal objectID As Long, abort As Long) ' If abort is set to True, the socket operation is aborted. abort = bAbort End Sub Private Sub Command1_Click() Set socket = New ChilkatSocket bAbort = False socket.UnlockComponent "anything for 30-day trial" ' Get heartbeat event callbacks every .2 seconds to assure that ' the component is not hung. The heartbeat event includes an abort ' flag that can be set by the application to abort the socket ' operation while in progress. socket.HeartbeatMs = 200 ' The Chilkat Socket component provides a convenience method to ' build simple HTTP Get requests. Dim getReq As String getReq = socket.BuildHttpGetRequest("http://www.htmlfeed.com/") ' Connect to the web server. success = socket.Connect("www.htmlfeed.com", 80, 5000) If (success = 0) Then MsgBox socket.LastErrorText Exit Sub End If ' Send the HTTP GET. success = socket.SendString(getReq) If (success = 0) Then MsgBox socket.LastErrorText Exit Sub End If ' Internal to the socket component, read in 40K chunks socket.ReceivePacketSize = 40000 ' Set the maximum idle time while reading to 10 seconds socket.MaxReadIdleMs = 10000 ' Read the 1st line of the HTTP response, which is the ' status line, such as "HTTP/1.1 200 OK" Dim status As String status = socket.ReceiveUntilMatch(vbCrLf) If (Len(status) = 0) Then MsgBox socket.LastErrorText Exit Sub End If StatusLine.Text = status ' Now get the HTTP response header, which ends in a double CRLF Dim responseHeader As String responseHeader = socket.ReceiveUntilMatch(vbCrLf & vbCrLf) If (Len(responseHeader) = 0) Then MsgBox socket.LastErrorText Exit Sub End If HeaderText.Text = responseHeader ' Use the Chilkat MIME ActiveX to parse the HTTP response header. Dim mime As New ChilkatMime mime.UnlockComponent "anything for 30-day trial" mime.LoadMime responseHeader ' Get the Content-Length header field. Dim contentLenStr As String contentLenStr = mime.GetHeaderField("content-length") Dim contentLen As Integer contentLen = Val(contentLenStr) ' Read the exact number of bytes specified by contentLen ' Note: Percentage completion event callbacks are available ' for socket receive operations where the number of bytes to ' be received is known in advance. Dim htmlPage As Variant htmlPage = socket.ReceiveBytesN(contentLen) ' We have the HTML page as a Variant containing byte data. ' As we all know, HTML pages can use any charset encoding, ' such as utf-8, iso-8859-1, windows-1252, shift_JIS, etc. ' We'll now draw upon the Chilkat Charset ActiveX to determine ' the charset of the HTML page and convert it to a string for ' display. Dim cc As New ChilkatCharset2 cc.UnlockComponent "anything for 30-day trial" CharsetText.Text = cc.GetHtmlCharset(htmlPage) If (Len(CharsetText.Text) = 0) Then ' No charset was specified within the HTML, assume windows-1252 CharsetText.Text = "windows-1252" End If cc.FromCharset = CharsetText.Text HtmlText.Text = cc.ConvertToUnicode(htmlPage) End Sub
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.