|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis code sample is intended to be used as a reference for handling the Working the WebRequest and WebResponseUsing the Dim request As WebRequest = WebRequest.Create("http://www.google.com")
Dim response As WebResponse = request.GetResponse()
Processing the Public Shared Function ProcessResponseStream( _
ByVal response As WebResponse, _
Optional ByVal defaultCharset As String = "utf-8") _
As Object
Dim st As IO.Stream
st = response.GetResponseStream()
Dim mem As New IO.MemoryStream()
Dim buffer(1024) As Byte
Dim read As Integer = 0
Try
' Read the response stream into memory
Do
read = st.Read(buffer, 0, buffer.Length)
mem.Write(buffer, 0, read)
Loop While read > 0
st.Close()
response.Close()
' Reset the memory position so we can read
' from the stream.
mem.Position = 0
' Parse the content type (ContentType is an
' internal class).
Dim contentType As ContentType
contentType = _
New ContentType(response.ContentType)
Select Case contentType.Type
Case "text"
' we should be able to read any text
' content into a string (assuming we
' have the correct encoding type).
Dim result(CInt(mem.Length)) As Byte
mem.Read(result, 0, CInt(mem.Length))
' We need to get the appropriate
' charset in order to decode the
' byte array as a string.
' This information can be sent
' in the Content-Type. If it isn't
' we need to use the default
' charset.
Dim charset As String
charset = contentType.GetValue( _
"charset", _
defaultCharset)
' We have the charset, now get the
' Encoding object and decode the
' content into a string.
Dim enc As Encoding
enc = Encoding.GetEncoding(charset)
Return enc.GetString(result)
Case "image"
' We should be able to read most image
' types directly into an image.
Return Image.FromStream(mem)
Case Else
' Let the caller figure out how to
' handle this content.
Dim result(CInt(mem.Length)) As Byte
mem.Read(result, 0, CInt(mem.Length))
Return result
End Select
Finally
mem.Close()
End Try
End Function
If a web resource requires authentication, you can provide it using the If you want to store the user's credentials between sessions, you need to make sure the data is secure. If you want to learn more about this, look into the DataProtection API in .NET. You can read more about it in my blog post on Protecting Data in .NET. Event-Based Asynchronous PatternThis is the same pattern that the The key to this pattern is the If you're interested in how I discovered this pattern, you can read my blog post on The Holy Grail of .NET Threading. BackgroundWebRequest ComponentThe NetLib project (included for download in this article), uses the Event-Based Asynchronous Pattern to provide a multi-threaded solution to downloading web content. It can be dropped onto a WinForm to be used like the The following code sample shows how the sample project uses the request key to process the Private mRequestKey As Object
Private Sub txtAddress_Validated( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles txtAddress.Validated
' make sure we cancel the last request
wrcBrowser.CancelRequest(mRequestKey)
If txtAddress.Text = "" Then
mRequestKey = Nothing
browser.DocumentText = ""
Else
' GetResponseAsyc returns an object that is
' used to identify the specific web request.
' The value must be stored in order to
' perform operations on it (such as cancelling)
mRequestKey = wrcBrowser.GetResponseAsync( _
txtAddress.Text)
End If
End Sub
Private Sub wrcBrowser_GetRequestCompleted( _
ByVal sender As System.Object, _
ByVal e As GetRequestCompletedEventArgs) _
Handles wrcBrowser.GetRequestCompleted
' we only care about the last request that we made
If e.RequestKey IsNot mRequestKey Then Return
' This method simply displays the status in the
' status bar.
SetStatus(e.Status, e.Error)
If e.Status = WebRequestStatus.Complete Then
' The request completed successfully, dump the
' html into the web browser.
browser.DocumentText = CStr(e.ResponseResults)
lblAddress.Text = e.Uri
End If
End Sub
A couple of notes about the sample code. The code has not gone through any sort of rigorous testing. At this point, it is simply a sample application. There are many different error conditions that I am not handling at this time that should be for production code (some of the omissions are intentional, others are not, you'll have to guess which ones are which :). Use the code at your own risk and if you find any major issues, please let me know.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||