1. Trang chủ
  2. » Công Nghệ Thông Tin

Professional C# 2008 phần 9 docx

185 254 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

c40.indd 1420c40.indd 1420 2/19/08 5:32:18 PM2/19/08 5:32:18 PM Part VI Communication Chapter 41: Accessing the Internet Chapter 42: Windows Communication Foundation Chapter 43: Windows Workflow Foundation Chapter 44: Enterprise Services Chapter 45: Message Queuing Chapter 46: Directory Services Chapter 47: Peer to Peer Networking Chapter 48: Syndication c41.indd 1421c41.indd 1421 2/19/08 5:32:32 PM2/19/08 5:32:32 PM c41.indd 1422c41.indd 1422 2/19/08 5:32:33 PM2/19/08 5:32:33 PM Accessing the Internet Chapters 37 through 39 discuss how you can use C# to write powerful, efficient, and dynamic Web pages using ASP.NET. For the most part, the clients accessing ASP.NET pages will be users running Internet Explorer or other Web browsers such as Opera or Firefox. However, you might want to add Web - browsing features to your own application, or you might 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. However, when you are accessing public Internet sites, you might not have any control over how the site is implemented. This chapter covers 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, this chapter covers: Downloading files from the World Wide Web Using the Web Browser control in a Windows Forms application Manipulating IP addresses and performing DNS lookups Socket programming with TCP, UDP, and socket classes This chapter covers some of the lower - level means of getting at these protocols through the .NET Framework. You will also find other means of communicating via these items using technologies, such as the Windows Communication Foundation (WCF), which is covered in the next chapter. The two namespaces of most interest for networking are System.Net and System.Net .Sockets . 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, whereas System.Net.Sockets contains classes to perform lower - level operations. You will find these classes 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. You will also find that some of the objects that this chapter works with are found in the System.IO namespace. This chapter takes a fairly practical approach, 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. ❑ ❑ ❑ ❑ c41.indd 1423c41.indd 1423 2/19/08 5:32:33 PM2/19/08 5:32:33 PM 1424 Part VI: Communication You will learn how to use the WebBrowser control in a Windows Forms environment. You will also learn how the WebBrowser control can make some specific Internet access tasks easier to accomplish. However, the chapter starts with the simplest case, sending a request to a server and storing the information sent back in the response. (As with other chapters, you can download the sample code for this chapter from the Wrox Web site at www.wrox.com .) The WebClient Class If you only want to request a file from a particular URI, then you will find that the easiest .NET class to use is System.Net.WebClient . This is an extremely high - level class designed to perform basic operations with only one or two commands. The .NET Framework currently supports URIs beginning with the http: , https: , and file: identifiers. It is worth noting that the term URL (Uniform Resource Locator) is no longer in use in new technical specifications, and URI (Uniform Resource Identifier) is now preferred. URI has roughly the same meaning as URL, but is a bit more general because URI does not imply you are using one of the familiar protocols, such as HTTP or FTP. Downloading Files Two methods are available for downloading a file using WebClient . The method you choose depends on how you want to process the file ’ s contents. If you simply want to save the file to disk, then you use the DownloadFile() method. This method takes two parameters: the URI of the file and a location (path and file name) to save the requested data: WebClient Client = new WebClient(); Client.DownloadFile(“http://www.reuters.com/”, “ReutersHomepage.htm”); More commonly, your application will want to process the data retrieved from the Web site. To do this, you use the OpenRead() method, which returns a Stream reference that you can then use to retrieve the data into memory: WebClient Client = new WebClient(); Stream strm = Client.OpenRead(“http://www.reuters.com/”); Basic Web Client Example The first example demonstrates the WebClient.OpenRead() method. You will display the contents of the downloaded page in a ListBox control. To begin, create a new project as a standard C# Windows Forms application and add a ListBox called listBox1 with the docking property set to DockStyle.Fill . At the beginning of the file, you will need to add the System.Net and System.IO namespaces references to your list of using directives. You then make the following changes to the constructor of the main form: public Form1() { InitializeComponent(); WebClient Client = new WebClient(); Stream strm = Client.OpenRead(“http://www.reuters.com”); StreamReader sr = new StreamReader(strm); string line; while ( (line=sr.ReadLine()) != null ) c41.indd 1424c41.indd 1424 2/19/08 5:32:33 PM2/19/08 5:32:33 PM 1425 Chapter 41: Accessing the Internet { listBox1.Items.Add(line); } strm.Close(); } In this example, you connect a StreamReader class from the System.IO namespace to the network stream. This allows you to obtain data from the stream as text through the use of higher - level methods, such as ReadLine() . This is an excellent example of the point made in Chapter 25 , “ Manipulating Files and the Registry, ” about the benefits of abstracting data movement into the concept of a stream. Figure 41 - 1 shows the results of running this sample code. Figure 41-1 The WebClient class also has an OpenWrite() method. This method returns a writable stream for you to send data to a URI. You can also specify the method used to send the data to the host; the default method is POST . The following code snippet assumes a writable directory named accept on the local machine. The code will create a file in the directory with the name newfile.txt and the contents Hello World : WebClient webClient = new WebClient(); Stream stream = webClient.OpenWrite(“http://localhost/accept/newfile.txt”, “PUT”); StreamWriter streamWriter = new StreamWriter(stream); streamWriter.WriteLine(“Hello World”); streamWriter.Close(); c41.indd 1425c41.indd 1425 2/19/08 5:32:34 PM2/19/08 5:32:34 PM 1426 Part VI: Communication Uploading Files The WebClient class also features UploadFile() and UploadData() methods. You use these methods when you need to post an HTML form or to upload an entire file. UploadFile() uploads a file to a specified location given the local file name, whereas UploadData() uploads binary data supplied as an array of bytes to the specified URI (there is also a DownloadData() method for retrieving an array of bytes from a URI): WebClient client = new WebClient(); client.UploadFile(“http://www.ourwebsite.com/NewFile.htm”, “C:\\WebSiteFiles\\NewFile.htm”); byte[] image; // code to initialize image so it contains all the binary data for // some jpg file client.UploadData(“http://www.ourwebsite.com/NewFile.jpg”, image); WebRequest and WebResponse Classes Although the WebClient class is very simple to use, it has very limited features. In particular, you cannot use it to supply authentication credentials — a particular problem with uploading data is that not many sites will accept uploaded files without authentication! It is possible to add header information to requests and to examine any headers in the response, but only in a very generic sense — there is no specific support for any one protocol. This is because WebClient is a very general - purpose class designed to work with any protocol for sending a request and receiving a response (such as HTTP or FTP). It cannot handle any features specific to any one protocol, such as cookies, which are specific to HTTP. To take advantage of these features, you need to use a family of classes based on two other classes in the System.Net namespace: WebRequest and WebResponse . You start off by seeing how to download a Web page using these classes. This is the same example as before, but using WebRequest and WebResponse . In the process, you will uncover the class hierarchy involved, and then see how to take advantage of extra HTTP features supported by this hierarchy. The following code shows the modifications you need to make to the BasicWebClient sample to use the WebRequest and WebResponse classes: public Form1() { InitializeComponent(); WebRequest wrq = WebRequest.Create(“http://www.reuters.com”); WebResponse wrs = wrq.GetResponse(); Stream strm = wrs.GetResponseStream(); StreamReader sr = new StreamReader(strm); string line; while ( (line = sr.ReadLine()) != null) { listBox1.Items.Add(line); } strm.Close(); } In the code example, you start by instantiating an object representing a Web request. You don ’ t do this using a constructor, but instead call the static method WebRequest.Create() . As you will learn in more detail later in this chapter, the WebRequest class is part of a hierarchy of classes supporting different c41.indd 1426c41.indd 1426 2/19/08 5:32:34 PM2/19/08 5:32:34 PM 1427 Chapter 41: Accessing the Internet network protocols. In order to receive a reference to the correct object for the request type, a factory mechanism is in place. The WebRequest.Create() method will create the appropriate object for the given protocol. The WebRequest class represents the request for information to send to a particular URI. The URI is passed as a parameter to the Create() method. A WebResponse represents the data you retrieve from the server. By calling the WebRequest.GetResponse() method, you actually send the request to the Web server and create a WebResponse object to examine the return data. As with the WebClient object, you can obtain a stream to represent the data, but in this case you use the WebResponse .GetResponseStream() method. Other WebRequest and WebResponse Features This section briefly discusses a few of the other areas supported by WebRequest , WebResponse , and other related classes. HTTP Header Information An important part of the HTTP protocol is the ability to send extensive header information with both request and response streams. This information can include cookies and the details of the particular browser sending the request (the user agent). As you would expect, the .NET Framework provides full support for accessing the most significant data. The WebRequest and WebResponse classes provide some support for reading the header information. However, two derived classes provide additional HTTP - specific information: HttpWebRequest and HttpWebResponse . As you will see in more detail later, creating a WebRequest with an HTTP URI results in an HttpWebRequest object instance. Because HttpWebRequest is derived from WebRequest , you can use the new instance whenever a WebRequest is required. In addition, you can cast the instance to an HttpWebRequest reference and access properties specific to the HTTP protocol. Likewise, the GetResponse() method call will actually return an HttpWebResponse instance as a WebResponse reference when dealing with HTTP. Again, you can perform a simple cast to access the HTTP - specific features. You can examine a few of the header properties by adding the following code before the GetResponse() method call: WebRequest wrq = WebRequest.Create(“http://www.reuters.com”); HttpWebRequest hwrq = (HttpWebRequest)wrq; listBox1.Items.Add(“Request Timeout (ms) = “ + wrq.Timeout); listBox1.Items.Add(“Request Keep Alive = “ + hwrq.KeepAlive); listBox1.Items.Add(“Request AllowAutoRedirect = “ + hwrq.AllowAutoRedirect); The Timeout property is specified in milliseconds, and the default value is 100,000 . You can set the Timeout property to control how long the WebRequest object will wait for the response before throwing a WebException . You can check the WebException.Status property to view the reason for an exception. This enumeration includes status codes for timeouts, connection failures, protocol errors, and more. The KeepAlive property is a specific extension to the HTTP protocol, so you access this property through an HttpWebRequest reference. KeepAlive allows multiple requests to use the same connection, saving time in closing and reopening connections on subsequent requests. The default value for this property is true . The AllowAutoRedirect property is also specific to the HttpWebRequest class. Use this property to control whether the Web request should automatically follow redirection responses from the Web server. Again, the default value is true . If you want to allow only a limited number of redirections, then set the MaximumAutomaticRedirections property of the HttpWebRequest to the desired number. c41.indd 1427c41.indd 1427 2/19/08 5:32:35 PM2/19/08 5:32:35 PM 1428 Part VI: Communication Although the request and response classes expose most of the important headers as properties, you can also use the Headers property itself to view the entire collection of headers. Add the following code after the GetResponse() method call to place all of the headers in the ListBox control: WebRequest wrq = WebRequest.Create(“http://www.reuters.com”); WebResponse wrs = wrq.GetResponse(); WebHeaderCollection whc = wrs.Headers; for(int i = 0; i < whc.Count; i++) { listBox1.Items.Add(string.Format(“Header {0} : {1}”, whc.GetKey(i), whc[i])); } This example code produces the list of headers shown in Figure 41 - 2 . Figure 41-2 Authentication Another property in the WebRequest class is the Credentials property. If you need authentication credentials to accompany your request, then you can create an instance of the NetworkCredential class (also from the System.Net namespace) with a username and password. You can place the following code before the call to GetResponse() . NetworkCredential myCred = new NetworkCredential(“myusername”, “mypassword”); wrq.Credentials = myCred; Working with Proxies You will find in enterprises that many firms must deal with a proxy server to make any type of HTTP or FTP request. Many times, the proxy server, which routes all of the organization ’ s requests and responses, uses some form of security (usually a username and a password). For your applications that use the WebClient or the WebRequest objects, you might need to take these proxy servers into account. As with the preceding NetworkCredential object, you are going to want to use the WebProxy object before you make a call to make the actual request. WebProxy wp = new WebProxy(“192.168.1.100”, true); wp.Credentials = new NetworkCredential(“user1”, “user1Password”); WebRequest wrq = WebRequest.Create(“http://www.reuters.com”); wrq.Proxy = wp; WebResponse wrs = wrq.GetResponse(); If you also require a designation of the user ’ s domain in addition to its credentials, then you would use a different signature on the NetworkCredential instantiation: c41.indd 1428c41.indd 1428 2/19/08 5:32:35 PM2/19/08 5:32:35 PM 1429 Chapter 41: Accessing the Internet WebProxy wp = new WebProxy(“192.168.1.100”, true); wp.Credentials = new NetworkCredential(“user1”, “user1Password”, “myDomain”); WebRequest wrq = WebRequest.Create(“http://www.reuters.com”); wrq.Proxy = wp; WebResponse wrs = wrq.GetResponse(); Asynchronous Page Requests An additional feature of the WebRequest class is the ability to request pages asynchronously. This feature is significant because there can be quite a long delay between sending a request to a host and receiving the response. Methods such as WebClient.DownloadData() and WebRequest .GetResponse() will not return until the response from the server is complete. You might not want your application frozen due to a long period of inactivity, and in such scenarios it is better to use the BeginGetResponse() and EndGetResponse() methods. BeginGetResponse() works asynchronously and returns almost immediately. Under the covers, the runtime will asynchronously manage a background thread to retrieve the response from the server. Instead of returning a WebResponse object, BeginGetResponse() returns an object implementing the IAsyncResult interface. With this interface, you can poll or wait for the response to become available and then invoke EndGetResponse() to gather the results. You can also pass a callback delegate into the BeginGetResponse() method. The target of a callback delegate is a method returning void and accepting an IAsyncResult reference as a parameter. When the worker thread is finished gathering the response, the runtime invokes the callback delegate to inform you of the completed work. As shown in the following code, calling EndGetResponse() in the callback method allows you to retrieve the WebResponse object: public Form1() { InitializeComponent(); WebRequest wrq = WebRequest.Create(“http://www.reuters.com”); wrq.BeginGetResponse(new AsyncCallback(OnResponse), wrq); } protected static void OnResponse(IAsyncResult ar) { WebRequest wrq = (WebRequest)ar.AsyncState; WebResponse wrs = wrq.EndGetResponse(ar); // read the response } Notice that you can retrieve the original WebRequest object by passing the object as the second parameter to BeginGetResponse() . The third parameter is an object reference known as the state parameter. During the callback method, you can retrieve the same state object using the AsyncState property of IAsyncResult . Displaying Output as an HTML Page The examples show how the .NET base classes make it very easy to download and process data from the Internet. However, so far, you have displayed files only as plain text. Quite often, you will want to view an HTML file in an Internet Explorer – style interface in which the rendered HTML allows you to see what the Web document actually looks like. Unfortunately, there is no .NET version of Microsoft ’ s Internet Explorer, but that does not mean that you cannot easily accomplish this task. Before the release of the .NET Framework 2.0, you could make reference to a COM object that was an encapsulation of Internet Explorer and use the .NET - interop capabilities to have aspects of your application work as a browser. Now, in the .NET Framework 2.0, as well as the .NET Framework 3.5, you can use the built - in WebBrowser control available for your Windows Forms applications. c41.indd 1429c41.indd 1429 2/19/08 5:32:36 PM2/19/08 5:32:36 PM [...]... 2/ 19/ 08 5:32:40 PM Chapter 41: Accessing the Internet Figure 41-8 System.Object System.MarshalByRefObject System.Net.WebRequest System.Net.WebResponse System.Net.HttpWebRequest System.Net.HttpWebRequest System.Net.FileWebRequest System.Net.FileWebRequest System.Net.FtpWebRequest System.Net.FtpWebRequest Third-Party Web Request Classes Third-Party Web Response Classes Figure 41 -9 14 39 c41.indd 14 39 2/ 19/ 08... to an IPAddress: IPAddress ipAddress = IPAddress.Parse(“234.56.78 .9 ); byte[] address = ipAddress.GetAddressBytes(); string ipString = ipAddress.ToString(); In this example, the byte integer address is assigned a binary representation of the IP address, and the string ipString is assigned the text “234.56.78 .9 1441 c41.indd 1441 2/ 19/ 08 5:32:41 PM Part VI: Communication IPAddress also provides a number... Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 2112)); listener.Listen(0); Socket socket = listener.Accept(); (continued) 14 49 c41.indd 14 49 2/ 19/ 08 5:32:45 PM Part VI: Communication (continued) Stream netStream = new NetworkStream(socket); StreamReader reader = new StreamReader(netStream); string result = reader.ReadToEnd(); Invoke(new... learned how to use the asynchronous capabilities in the networking classes, which give a Windows Forms application the professional touch of a responsive user interface Now you move on to learning about Windows Communication Foundation 1453 c41.indd 1453 2/ 19/ 08 5:32:47 PM c41.indd 1454 2/ 19/ 08 5:32:47 PM Windows Communication Foundation Previous to NET 3.0, several communication technologies were required... resulting document In Visual Studio 2008 Designer, your application should look as shown in Figure 41-3 With this application, when the end user types a URL and presses Enter, this key press will register with the application Then the WebBrowser control will go off to retrieve the requested page, subsequently displaying it in the control itself Figure 41-3 1430 c41.indd 1430 2/ 19/ 08 5:32:36 PM Chapter 41:... false; if (webBrowser1.CanGoBack) { buttonBack.Enabled = true; } else { buttonBack.Enabled = false; } if (webBrowser1.CanGoForward) { buttonForward.Enabled = true; } (continued) 1435 c41.indd 1435 2/ 19/ 08 5:32: 39 PM Part VI: Communication (continued) else { buttonForward.Enabled = false; } } } } Many different activities are going on in this example because there are so many options for the end user when... these classes work, you need to build two applications Figure 41-11 shows the first application, TcpSend This application opens a TCP connection to a server and sends the C# source code for itself Figure 41-11 Once again, you create a C# Windows application The form consists of two text boxes (txtHost and txtPort) for the host name and port, respectively, as well as a button (btnSend) to click and start... buttonBack.Enabled = true; } else { buttonBack.Enabled = false; } } private void webBrowser1_CanGoForwardChanged(object sender, EventArgs e) { if (webBrowser1.CanGoForward == true) 1436 c41.indd 1436 2/ 19/ 08 5:32: 39 PM Chapter 41: Accessing the Internet { buttonForward.Enabled = true; } else { buttonForward.Enabled = false; } } Run the project now, visit a Web page, and click through a few links You should... print it without even displaying the loaded document This can be accomplished as shown here: WebBrowser wb = new WebBrowser(); wb.Navigate(“http://www.wrox.com”); wb.Print(); 1437 c41.indd 1437 2/ 19/ 08 5:32: 39 PM Part VI: Communication Displaying the Code of a Requested Page In the beginning of this chapter, you used the WebRequest and the Stream classes to get at a remote page to display the code of... private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { textBox1.Text = webBrowser1.Url.ToString(); this.Text = webBrowser1.DocumentTitle.ToString(); } 1434 c41.indd 1434 2/ 19/ 08 5:32:38 PM Chapter 41: Accessing the Internet private void Form1_Load(object sender, EventArgs e) { buttonBack.Enabled = false; buttonForward.Enabled = false; buttonStop.Enabled = false; } private . 1421c41.indd 1421 2/ 19/ 08 5:32:32 PM2/ 19/ 08 5:32:32 PM c41.indd 1422c41.indd 1422 2/ 19/ 08 5:32:33 PM2/ 19/ 08 5:32:33 PM Accessing the Internet Chapters 37 through 39 discuss how you can use C# to write. 41-8 System.Object System.MarshalByRefObject System.Net.WebResponseSystem.Net.WebRequest System.Net.HttpWebRequestSystem.Net.HttpWebRequest System.Net.FileWebRequestSystem.Net.FileWebRequest System.Net.FtpWebRequestSystem.Net.FtpWebRequest Third-Party Web Request Classes Third-Party Web Response Classes Figure 41 -9 c41.indd 1439c41.indd 14 39 2/ 19/ 08 5:32:40 PM2/ 19/ 08 5:32:40 PM . WebBrowser control available for your Windows Forms applications. c41.indd 1429c41.indd 14 29 2/ 19/ 08 5:32:36 PM2/ 19/ 08 5:32:36 PM 1430 Part VI: Communication The WebBrowser control encapsulates

Ngày đăng: 12/08/2014, 23:23

TỪ KHÓA LIÊN QUAN