Chapter 32: Visual Basic and the Internet


In today’s network-centric world, it’s very likely that applications will need to work with other computers over a private network, the Internet, or both. This chapter details how to do the following:

  • Download resources from the Web

  • Design your own communication protocols

  • Reuse Internet Explorer in your applications

Downloading Internet Resources

Downloading content from the Web is very easy, so you’ll throw together a basic application before getting into some meatier topics. This application downloads HTML from a Web page and displays it in a text box. Later, you’ll learn how you can display HTML properly by hosting Internet Explorer (IE) directly using the new WebBrowser control in Windows Forms applications, but for now you’ll just use plain text.

In order to download a Web page, you need to be able to identity the remote page that you wish to download, make a request of the Web server that can provide that page, listen for the response, and download the data for the resource.

The relevant classes for this example are System.Uri, System.Net.WebRequest, System.Net.HttpWebRequest, and System.Net.HttpWebResponse:

  • System.Uri is a useful general-purpose class for expressing a Uniform Resource Identifier (URI). A Uniform Resource Locator (URL) is a type of URI (although in reality the terms are so confused that they are often used interchangeably). A URI, however, is “more than” a URL, which is why this .NET class is Uri and not Url. System. You’ll find that Uri has many properties for decoding a URI. For example, if you had a string like www.pretendcompany.com:8080/ myservices/myservice.asmx?WSDL, you could use the Port property to extract the port number, the Query property to extract the query string, and so on.

  • A WebRequest expresses some kind of Internet resource, whether it is located on the LAN or WAN. (A better name for this class would be NetRequest, as the classes aren’t specifically related to the Web protocol.)

  • Protocol-specific descendants of WebRequest carry out the actual request: HttpWebRequest expresses an HTTP download and FileWebRequest expresses a file download - for example, file://c:/MyFile.txt.

  • An HttpWebResponse is returned once a connection to the Web server has been made and the resource is available to download.

There are another two major classes related to working with the Internet in the .NET Framework: System.Net.WebClient and System.Net.WebProxy. WebClient, the latter being a helper class that wraps the request and response classes previously mentioned.

Because this is a professional-level book, this example shows you what to do behind the scenes - in effect, re-engineer what WebClient can do. You’ll look at WebProxy later, which enables you to explicitly define a proxy server to use for Internet communications.

Let’s use these classes to build an application. Create a new Windows application, create a new form, and add controls to it as shown in Figure 32-1.

image from book
Figure 32-1

The control names are textUrl, buttonGo, and textData. The Anchor properties of the controls are set so that the form resizes properly. The textUrl should be set to Top, Left, Right. Set buttonGo to Top, Right, and set textData to Top, Left, Bottom, Right.

Add these namespace import declarations to the form’s code:

  Imports System.IO Imports System.Net Imports System.Text 

To keep the code simple, you’ll include all the functionality into the Click handler of buttonGo. Ideally, you want to break the code in the handler out to a separate method. This enriches the interface of the object and promotes good reuse.

The first thing you do here is create a new System.Uri based on the URL that the user enters into the text box:

  Private Sub buttonGo_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles buttonGo.Click    Dim uri As New Uri(textUrl.Text) 

Then, illustrate some of the useful properties of System.Uri:

  Dim builder As New StringBuilder() builder.Append("AbsolutePath: " & uri.AbsolutePath & VbCrLf) builder.Append("AbsoluteUri: " & uri.AbsoluteUri & VbCrLf) builder.Append("Host: " & uri.Host & VbCrLf) builder.Append("HostNameType: " & uri.HostNameType.ToString() & _                 VbCrLf) builder.Append("LocalPath: " & uri.LocalPath & VbCrLf) builder.Append("PathAndQuery: " & uri.PathAndQuery & VbCrLf) builder.Append("Port: " & uri.Port & VbCrLf) builder.Append("Query: " & uri.Query & VbCrLf) builder.Append("Scheme: " & uri.Scheme) MessageBox.Show(builder.ToString()) 

The shared Create method of System.Net.WebRequest is used to create the actual object that you can use to download the Web resource. Note that you don’t create an instance of HttpWebRequest; you’re working with a return object of type WebRequest. However, you’ll actually be given an HttpWebRequest object, and WebRequest chooses the most appropriate class to return based on the URI. This enables you to build your own handlers for different network resources that can be used by consumers who simply supply an appropriate URL.

To make the request and get the response back from the server (so ultimately you can access the data), you call the GetResponse method of WebRequest. In this case, you’ll get an HttpWebResponse object - again, it’s up to the implementation of the WebRequest-derived object, in this case HttpWebRequest, to return an object of the most suitable type.

If the request is not OK, you’ll get an exception (which for the sake of simplicity you won’t bother processing). If the request is OK, you can get the length and type of the response using properties of the WebResponse object:

  Dim request As WebRequest = WebRequest.Create(uri) Dim response As WebResponse = request.GetResponse() builder = New StringBuilder() builder.Append("Request type: " & request.GetType().ToString() & VbCrLf) builder.Append("Response type: " & response.GetType().ToString() & VbCrLf) builder.Append("Content length: " & response.ContentLength & _         " bytes" & VbCrLf) builder.Append("Content type: " & response.ContentType & VbCrLf) MessageBox.Show(builder.ToString()) 

It just remains for you to download the information. You can do this through a stream (WebResponse objects return a stream by overriding GetResponseStream); moreover, you can use a System.IO.StreamReader to download the whole lot in a single call by calling the ReadToEnd method. This method only downloads text, so if you want to download binary data you have to use the methods on the Stream object directly, or use a System.IO.BinaryReader.

   Dim stream As Stream = response.GetResponseStream()  Dim reader As New StreamReader(stream)  Dim data As String = reader.ReadToEnd()  reader.Close()  stream.Close()  textData.Text = data End Sub 

If you run the application, enter a URL of www.reuters.com, and click Go, you’ll see debugging information about the URL, as shown in Figure 32-2.

image from book
Figure 32-2

This is a simple URL. The application tells you that the scheme is http and the host name type is Dns. If you enter an IP into the URL to be requested, rather than a host name, then this type will come back as IPv4. This tells you where the host name came from; in this case, it’s a general Internet host name. Next, the application provides information about the response (see Figure 32-3).

image from book
Figure 32-3

The response data itself is shown in Figure 32-4.

image from book
Figure 32-4

Perhaps the most important exception to be aware of when using these classes is the System.Net.WebException exception. If anything goes wrong on the WebRequest.GetResponse call, this exception is thrown. Among other things, this exception provides access to the WebResponse object through the Response property. The StatusCode property of WebResponse tells you what actually happened through the HttpStatusCode enumeration. For example, HttpStatusCode.NotFound is the equivalent of the HTTP 404 status code.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net