First, let's introduce the HTTP protocol, which is short for hpyer text Transfer Protocal. It is the most important network protocol on the modern Internet. Hypertext Transfer Protocol is located at the TCP/IP protocol application layer, is a simple and fast C/S-oriented protocol. The HTTP process is divided into four steps: connection, request, response, and disconnection. C # language provides good support for the HTTP protocol. net Class Library provides the webrequest and webresponse classes, both of which are included in the system. in the. NET namespace, using these two classes can implement many advanced network functions. In this article, multi-threaded file downloads are implemented using these two classes. Both webrequest and webresponse are abstract base classes. Therefore, they cannot be used directly as objects in a program and must be inherited. In actual use, you can select appropriate subclasses Based on the URI prefix in the URI parameter, for Uris such as HTTP, The httpwebrequest and httpwebresponse classes can be used to process HTTP Communication between client programs and web servers.
The httpwebrequest class provides many advanced functions for accessing files on Web Servers through HTTP. The httpwebrequest class supports attributes and methods defined in webrequest. httpwebrequest exposes the values of public HTTP headers sent to Internet resources as attributes, which are set by methods or systems, common HTTP headers set by attributes or methods are: Accept, set by accept, connect, set by connection and keepalive attributes, and Content-Length, set by contentlength, content-Type, set by the contenttype attribute, range, set by the addrange method. in actual use, the header information is correctly set and then transmitted to the Web server. The web server responds as required.
The httpwebresponse class inherits from the webresponse class and specifically handles HTTP responses returned from the Web server. This class implements many methods and has many attributes that can fully process received Internet information. In the httpwebresponse class, for most common HTTP header fields, there are independent attributes corresponding to them. Through these attributes, programmers can conveniently access the information in the header field of the HTTP received message, in this example, the httpwebresponse class attribute is: contentlength, which is the length of the received content.
With the above understanding, let's take a look at the usage of these two classes. To create an httpwebrequest object, do not directly use the HTTP webrequest constructor, but use webrequest. the create method initializes an httpwebrequest instance, for example:
Httpwebrequest HWR = (httpwebrequest) webrequest. Create (http://www.163.com /);
After this object is created, you can set the content of many HTTP header fields through the httpwebrequest attribute, such as HWR. addrange (,); set the receiving object range to-bytes.
When the httpwebreques object uses the getresponse () method, an httpwebresponse object is returned. To propose HTTP Response Message Information, you must use the getresponsestream () method of httpwebresponse to return a stream object, you can read the packets returned by HTTP. For example, you must first define a strean object public system. io. stream ns; then NS = HWR. getresponse (). getresponsestream (); To create a stream object.