Share via


Playing with System.NET

The System.NET namespace provides a simple programming interface for many of the common networking protocols. The WebRequest and WebResponse classes form the basis of what has become known as “pluggable protocols” This is .NET Framework 2.0 concept that is designed to enable applications to use network resources without having to worry about the specific details of the protocols that each resource uses.

For applications that need to make a simple request for Internet resources, the System.Net.WebClient class provides the most common methods for uploading or downloading data from an Internet server. The WebClient uses the WebRequest class to provide access to Internet resource.


Figure 1: The sample application

One of the things that the WebClient can do is retrieve the basic information about the various networking protocols. For example, if you want to retrieve the content and header data from an HTTP request using the application in Figure 1 we can do this using the following code that is placed behind the ‘Get Data’ button.

' write to the browser
Dim wc As New WebClient
Dim content As String = wc.DownloadString(TxtUrl.Text)

' display content
TxtData.Text = content
WbBrowser.DocumentText = content

' display headers
Dim wcHeaders As New WebHeaderCollection
wcHeaders = wc.ResponseHeaders

Dim wcInfo As String
For Each wcInfo In wcHeaders.Keys
   Txtheader.Text = Txtheader.Text & wcInfo.ToString & " - " & _
   wcHeaders.Item(wcInfo.ToString) & vbNewLine
Next

Essentially when the URL is called the data is retrieved as shown in Figure 2.


Figure 2: The completed application

For further reference I have placed this sample application here for download.
<<Download the sample application>>

Comments