Many people have experienced the use of network ant financial or network express files on the Internet. The use of these software can greatly speed up the transmission of files on the Internet and reduce the time for file transmission. Why are these software so powerful? The main reason is that these software uses multi‑thread download and resumable data transfer. If we compile a program like this and can quickly download files on the internet, it must be a pleasure. Next I will explain how to use C # To compile a program that supports multi-thread File Download. You will see how easy it is to use C # To compile a network application, you can also learn about the powerful network functions in C.
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 excellent support for the HTTP protocol, and provides the WebRequest and WebResponse classes in the class library. Both classes 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. With the above preparation knowledge, we start to design our multi-threaded Internet File Download program, first open.. Net integrated development environment, select "file", "new", and "project", then select "Visual C # Project", and select "Windows application" in the list box on the right of the wizard ", enter the project name, for example, httpftp, and then click OK. The wizard automatically generates a Windows application project. First, open the window designer design application window and add the following controls:
A listBox1 three text labels label1-label3 three text boxes textBox1-textBox3 a start receiving button button1 designed window such:
The control definition code is:
- public System.Windows.Forms.ListBox listBox1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.TextBox textBox1
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.TextBox textBox2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.TextBox textBox3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.TextBox textBox4;
Open the Form1 code editor and add the following namespace:
- Using System. Net; // Network Function
- Using System. IO; // stream support
- Using System. Threading; // thread support
Add the following program variables:
- Public bool [] threadw; // indicates the end of each thread.
- Public string [] filenamew; // file name of the file received by each thread
- Public int [] filestartw; // start position of the file received by each thread
- Public int [] filesizew; // the size of the file received by each thread
- Public string strurl; // URL of the accepted File
- Public bool hb; // file merging mark
- Public int thread; // number of processes
Define an HttpFile class to manage the receiving thread. The Code is as follows:
- Public class HttpFile
- {
- Public Form1 formm;
- Public int threadh; // thread code
- Public string filename; // file name
- Public string strUrl; // the URL of the received File
- Public FileStream fs;
- Public HttpWebRequest re