Chapter 20 - Accessing the Internet | |
bySimon Robinsonet al. | |
Wrox Press 2002 | |
In can use C# to write powerful, efficient, and dynamic web pages using ASP.NET, as well as web services. For the most part, the clients accessing ASP.NET pages will be users running Internet Explorer or other web browsers. However, you might want to add web-browsing features to your own application, or need your applications to programmatically obtain information from a web site. In this latter case, it is usually better for the site to implement a web service - but if you are accessing outside sites you may not have any control over how the site is implemented.
In this chapter, we will cover facilities provided through the .NET base classes for using various network protocols, particularly HTTP and TCP, to access networks and the Internet as a client. In particular, we will cover:
Downloading files from the World Wide Web
Using Internet Explorer as an ActiveX control
Manipulating IP addresses and performing DNS lookups
Socket programming with TCP, UDP, and socket classes
The two namespaces we are most interested in for networking are the System.Net and the System.Net.Sockets namespaces. The System.Net namespace is generally concerned with higher-level operations, for example, downloading and uploading files, and making web requests using HTTP and other protocols, while System.Net.Sockets contains classes to perform lower-level operations. You will find these classes more useful when you want to work directly with sockets or protocols such as TCP/IP. The methods in these classes closely mimic the Windows socket (Winsock) API functions derived from the Berkeley sockets interface.
We are going to take a fairly practical approach in this chapter, mixing examples with a discussion of the relevant theory and networking concepts as appropriate. This chapter is not a guide to computer networking, but an introduction to using the .NET Framework for network communication.
We will start with the simplest case of sending a request to a server and storing the information sent back.