Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#

.pdf
Скачиваний:
175
Добавлен:
12.02.2016
Размер:
16.87 Mб
Скачать

Module 11: Internet Access

9

 

 

 

Casting WebRequest Objects to HttpWebRequest

To handle HTTP protocol requests to the Internet, the .NET Framework provides an HttpWebRequest class that is derived from the WebRequest class. In most cases, the WebRequest class provides all of the properties that you need to make a request. However, if you need to access the HTTP protocol-specific properties of the request, you can typecast WebRequest objects that are created by the WebRequest.Create to HttpWebRequest, as in the following example:

HttpWebRequest httpReq = (HttpWebRequest)

WebRequest.Create("http://www.contoso.com/");

// Turn off connection keep-alives. httpReq.KeepAlive = false;

Supporting Additional Protocols

The .NET Framework provides protocol-specific WebRequest and

WebResponse descendants for URIs that begin with http:, https:, and file:.

The programmable pluggable protocols of System.Net allow applications to provide access through other protocols. To support additional protocols, you should implement protocol-specific descendants of WebRequest and WebResponse and register the descendant’s constructor with the

WebRequest.RegisterPrefix method.

For more information about supporting additional protocols, see the .NET

Framework Software Development Kit (SDK) documentation.

10

Module 11: Internet Access

Invoking a WebRequest

Topic Objective

To explain how to invoke the request for an Internet resource by calling the

GetResponse method on the WebRequest instance.

Lead-in

After creating the

WebRequest, you invoke the request for the Internet resource by calling the

GetResponse method on the WebRequest instance.

! Request Is Made by Calling the GetResponse Method

WebResponse resp = req.GetResponse();

WebResponse resp = req.GetResponse();

" Cast to access HTTP-specific features

HttpWebResponse httpResp =

HttpWebResponse httpResp =

(HttpWebResponse)httpReq.GetResponse();

(HttpWebResponse)httpReq.GetResponse();

//Get the HTTP content length returned by the server. //Get the HTTP content length returned by the server. String contentLength =

String contentLength = httpResp.ContentLength.ToString(); httpResp.ContentLength.ToString();

*****************************ILLEGAL FOR NON-TRAINER USE******************************

After creating the WebRequest, you invoke the request for the Internet resource by calling the GetResponse method on the WebRequest instance. The GetResponse method is responsible for:

!Constructing the protocol-specific request from the properties of the

WebRequest instance.

!Making the TCP or UDP socket connection to the server.

!Sending the request.

The GetResponse method returns an instance that is derived from WebResponse that matches the instance that is derived from WebRequest, as in the following example:

WebResponse resp = req.GetResponse();

The WebResponse class is also an abstract class that defines properties and methods that are available to all applications that use pluggable protocols. WebResponse descendents are responsible for implementing these properties and methods for the underlying protocol.

For example, the HttpWebResponse class implements the WebResponse class for the HTTP protocol. If you need to access the HTTP protocol-specific properties of the response, you can typecast WebResponse objects to HttpWebResponse, as in the following example:

HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

//Get the HTTP content length returned by the server. String contentLength = httpResp.ContentLength.ToString();

Module 11: Internet Access

11

 

 

 

Sending Data

Topic Objective

To describe how requests that send data to the server are created.

Lead-in

For requests that send data to the server, such as HTTP-POST or FTP-PUT requests, the data is sent through a network stream that is provided by the

WebRequest.GetRequest Stream method.

! For Requests That Send Data to the Server

// ...

// ...

try try

{{

byte[] sendData = byte[] sendData =

Encoding.ASCII.GetBytes("some data");

Encoding.ASCII.GetBytes("some data");

int sendLength = sendData.Length; int sendLength = sendData.Length; HttpWebRequest httpReq = HttpWebRequest httpReq = (HttpWebRequest) WebRequest.Create( (HttpWebRequest) WebRequest.Create(

"http://www.contoso.com/");

"http://www.contoso.com/"); httpReq.Method = "POST"; httpReq.Method = "POST"; httpReq.ContentLength = sendLength; httpReq.ContentLength = sendLength;

Stream sendStream = httpReq.GetRequestStream(); Stream sendStream = httpReq.GetRequestStream(); sendStream.Write(sendData,0,sendLength); sendStream.Write(sendData,0,sendLength); sendStream.Close();

}}

sendStream.Close();

catch(Exception e) {//...} //...

catch(Exception e) {//...} //...

*****************************ILLEGAL FOR NON-TRAINER USE******************************

For requests that send data to the server, such as HTTP-POST or FTP-PUT requests, the data is sent through a network stream that is provided by the

WebRequest.GetRequestStream method. You use the resulting Stream object to write the data. When you have finished uploading, you must close the request stream with the Stream.Close method.

The following example shows how to create a request that uses HTTP-POST to send data to the server:

// ...

try

{

byte[] sendData = Encoding.ASCII.GetBytes("some data"); int sendLength = sendData.Length;

HttpWebRequest httpReq = (HttpWebRequest) WebRequest.Create(

"http://www.contoso.com/"); httpReq.Method = "POST"; httpReq.ContentLength = sendLength;

Stream sendStream = httpReq.GetRequestStream(); sendStream.Write(sendData,0,sendLength); sendStream.Close();

}

catch(Exception e) {//...

}

//...

After closing the stream, you can call GetResponse to ensure that the server received the data correctly.

12

Module 11: Internet Access

Receiving Data

Topic Objective

To explain how to obtain the stream that contains response data from the network.

Lead-in

To obtain the stream that contains response data from the network resource, use the GetResponseStream method of the

WebResponse instance.

! Reading Response Data

// Get the response stream. // Get the response stream.

Stream respstrm = resp.GetResponseStream(); Stream respstrm = resp.GetResponseStream(); // Create a buffer to hold the response data. // Create a buffer to hold the response data. int BufferSize = 512;

int BufferSize = 512;

Byte[] Buffer = new Byte[BufferSize]; Byte[] Buffer = new Byte[BufferSize]; // Read the stream to access the data. // Read the stream to access the data.

int bytesRead = respstrm.Read(Buffer, 0, BufferSize); int bytesRead = respstrm.Read(Buffer, 0, BufferSize); while (bytesRead > 0) {

while (bytesRead > 0) { Console.Write( Console.Write(

Encoding.ASCII.GetString(Buffer, 0, bytesRead)); Encoding.ASCII.GetString(Buffer, 0, bytesRead));

}}

bytesRead = respstrm.Read(Buffer, 0, BufferSize); bytesRead = respstrm.Read(Buffer, 0, BufferSize);

respstrm.Close();

respstrm.Close();

*****************************ILLEGAL FOR NON-TRAINER USE******************************

To obtain the stream that contains response data from the network resource, use the GetResponseStream method of the WebResponse instance, as in the following example:

// Get the response stream.

Stream respstrm = resp.GetResponseStream();

//Create a buffer to hold the response data. int BufferSize = 512;

Byte[] Buffer = new Byte[BufferSize];

//Read the stream to access the data.

int bytesRead = respstrm.Read(Buffer, 0, BufferSize); while (bytesRead > 0) {

Console.Write(

Encoding.ASCII.GetString(Buffer, 0, bytesRead)); bytesRead = respstrm.Read(Buffer, 0, BufferSize);

}

respstrm.Close();

If your application requires only the header information that is returned in the WebResponse and ignores any returned data, then you do not need to get the response stream.

Closing Responses

After reading the data from the response, you must close any opened stream by using the Stream.Close method or close the response by using the WebResponse.Close method, as in the following example:

resp.Close();

Module 11: Internet Access

13

 

 

 

You do not have to call the Close method on both the response stream and the WebResponse instance, but it is recommended. WebResponse.Close calls Stream.Close when it closes the response. If you do not close each response, your application will run out of connections to the server and be unable to process additional requests.

Considerations for Working with NetworkStreams

When using streams from network resources, remember the following:

!Because the NetworkStream class cannot change position in the stream, the CanSeek property always returns false. The Seek and Position methods throw a NotSupportedException.

!When you use WebRequest and WebResponse, stream instances that are created by calling GetResponseStream are read-only, and stream instances that are created by calling GetRequestStream are write-only.

!The call to GetResponse may block if network resources are not available.

You should consider using an asynchronous request with the

BeginGetResponse and EndGetResponse methods.

!The call to GetRequestStream may block while the connection to the server is created.

You should consider using an asynchronous request for the stream with the

BeginGetRequestStream and EndGetRequestStream methods. Asynchronous operations are beyond the scope of this course.

!You can use the StreamReader class to make the task of encoding easier.

The following code example uses a StreamReader to read an ASCIIencoded stream from a WebResponse instance. The creation of the request is not shown.

// Create a response object.

WebResponse response = request.GetResponse();

//Get a readable stream from the server. StreamReader sr =

new StreamReader(

response.GetResponseStream(), Encoding.ASCII);

//Use the stream. Remember when you are through

//with the stream to close it

//...

sr.Close();

14

Module 11: Internet Access

Using the WebRequest and WebResponse Model

Topic Objective

To provide an example of the WebRequest and WebResponse model.

Lead-in

You will now see an example of the use of the WebRequest and WebResponse model as discussed in this section.

//...

//...

WebRequest wReq = WebRequest.Create

WebRequest wReq = WebRequest.Create ("http://localhost/postinfo.html"); ("http://localhost/postinfo.html");

WebResponse wResp = wReq.GetResponse(); WebResponse wResp = wReq.GetResponse(); // Get a readable stream from the server // Get a readable stream from the server StreamReader sr = new StreamReader( StreamReader sr = new StreamReader(

wResp.GetResponseStream(),Encoding.ASCII);

wResp.GetResponseStream(),Encoding.ASCII); int length = 1024;

int length = 1024;

char[] Buffer = new char[1024]; char[] Buffer = new char[1024]; int bytesread = 0;

int bytesread = 0;

//Read from the stream and write data to console //Read from the stream and write data to console bytesread = sr.Read( Buffer, 0, length); bytesread = sr.Read( Buffer, 0, length);

while( bytesread > 0 ) { while( bytesread > 0 ) {

Console.Write( Buffer,0, bytesread); Console.Write( Buffer,0, bytesread); bytesread = sr.Read( Buffer, 0, length);

}} bytesread = sr.Read( Buffer, 0, length);

//Close the stream when finished //Close the stream when finished sr.Close();

sr.Close();

//...

//...

*****************************ILLEGAL FOR NON-TRAINER USE******************************

The following code example fully demonstrates the use of the WebRequest and WebResponse model as discussed in this section:

using System; using System.Net; using System.IO; using System.Text;

class App

{

public static void Main(string[] args)

{

try

{

WebRequest wReq = WebRequest.Create ("http://localhost/postinfo.html");

WebResponse wResp = wReq.GetResponse();

// Get a readable stream from the server StreamReader sr = new StreamReader(

wResp.GetResponseStream(),Encoding.ASCII);

int length = 1024;

char[] Buffer = new char[1024]; int bytesread = 0;

(Code continued on the following page.)

Module 11: Internet Access

15

 

 

 

//Read from the stream and write data to console bytesread = sr.Read( Buffer, 0, length);

while( bytesread > 0 )

{

Console.Write( Buffer,0, bytesread); bytesread = sr.Read( Buffer, 0, length);

}

//Close the stream when finished sr.Close();

}

catch(Exception e)

{

Console.WriteLine(

"\r\nThe request URI could not be found or was! malformed:\n {0}",

e.ToString());

}

}

}

16

Module 11: Internet Access

# Application Protocols

Topic Objective

To introduce the topics in the section.

Lead-in

The .NET Framework supports application protocols that are currently in common use on the Internet.

!HTTP

!Internet Domain Name System

!TCP and UDP

!Sockets

*****************************ILLEGAL FOR NON-TRAINER USE******************************

The .NET Framework supports application protocols that are currently in common use on the Internet.

This section provides information about using the HTTP, TCP, and UDP protocol support that is provided in the .NET Framework, in addition to information about using the Windows Sockets interface to implement custom protocols.

Module 11: Internet Access

17

 

 

 

HTTP

Topic Objective

To describe the support that the .NET Framework provides for the HTTP protocol.

Lead-in

With the HttpWebRequest and HttpWebResponse classes, the .NET Framework provides comprehensive support for the HTTP protocol, which makes up the majority of all Internet traffic.

!Classes That Provide HTTP and HTTPS Protocols

"HttpWebRequest and HttpWebResponse

!Support Most HTTP 1.1 features

!HTTP Redirects Automatically if AllowAutoRedirect Property Is true (the Default)

!Use ServicePoint, ServicePointManager, and ConnectionGroupName Classes to Manage Connections

*****************************ILLEGAL FOR NON-TRAINER USE******************************

With the HttpWebRequest and HttpWebResponse classes, the .NET Framework provides comprehensive support for the HTTP protocol, which makes up the majority of all Internet traffic.

Classes for the HTTP and HTTPS Protocols

The HttpWebRequest and HttpWebResponse classes are derived from the WebRequest and WebResponse classes, as was described in Creating a WebRequest in this module. HttpWebRequest and HttpWebResponse are returned by default whenever the static method WebRequest.Create encounters a URI that begins with http or https.

In most cases, the WebRequest and WebResponse classes provide all that is necessary to make the request, but if you need access to the HTTP-specific features that are exposed as properties, you can typecast these classes to

HttpWebRequest or HttpWebResponse, as demonstrated in Creating a WebRequest in this module.

Note Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest instances. If the scheme for the URI is http:// or https://, Create returns an

HttpWebRequest instance.

Support for HTTP 1.1 Features

The HttpWebRequest and HttpWebResponse classes encapsulate a standard HTTP request/response transaction and provide access to common HTTP headers. These classes also support most HTTP 1.1 features, including pipelining, chunking, authentication, pre-authentication, encryption, proxy support, server certificate validation, and connection management. Custom headers and headers that are not provided through properties can be stored in and accessed through the Headers property.

18

Module 11: Internet Access

Support for HTTP Redirects

You can make your application follow HTTP redirects automatically by setting the AllowAutoRedirect property to true, which is the default value. The application redirects requests, and the ResponseURI property of HttpWebResponse contains the actual Internet resource that responds to the request. If you set AllowAutoRedirect to false, your application must be able to handle redirects as HTTP protocol errors.

Applications receive HTTP protocol errors by catching a WebException with the value of the Status property set to WebExceptionStatus.ProtocolError. The Response property contains the WebResponse that is sent by the server and indicates the actual HTTP error that is encountered.

Managing Internet Connections

Applications that use HTTP to connect to data resources can use the .NET Framework ServicePoint and ServicePointManager classes to manage the number of connections to the Internet and to optimize scale and performance. The number of connections between a client and server can have a dramatic effect on application throughput.

You can use ConnectionGroupName to form connection grouping that associates specific requests within a single application to a defined connection pool. You may have to use this technique with a middle-tier application that connects to a back-end server on behalf of a user and uses an authentication protocol that supports delegation, such as Kerberos, or by a middle-tier application that supplies its own credentials.

For more information about classes that are used to manage connections and their methods, see the .NET Framework SDK documentation.

Соседние файлы в папке c#