C # implement HTTP protocol: multi-thread file transmission

Source: Internet
Author: User
Tags network function

 

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:

 
 
  1. 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:

 
 
  1. public System.Windows.Forms.ListBox listBox1;  
  2. private System.Windows.Forms.Label label1;  
  3. private System.Windows.Forms.TextBox textBox1  
  4. private System.Windows.Forms.Button button1;  
  5. private System.Windows.Forms.Label label2;  
  6. private System.Windows.Forms.TextBox textBox2;  
  7. private System.Windows.Forms.Label label3;  
  8. private System.Windows.Forms.TextBox textBox3;  
  9. private System.Windows.Forms.Label label4;  
  10. private System.Windows.Forms.TextBox textBox4;  

Open the Form1 code editor and add the following namespace:

 
 
  1. Using System. Net; // Network Function
  2. Using System. IO; // stream support
  3. Using System. Threading; // thread support

Add the following program variables:

 
 
  1. Public bool [] threadw; // indicates the end of each thread.
  2. Public string [] filenamew; // file name of the file received by each thread
  3. Public int [] filestartw; // start position of the file received by each thread
  4. Public int [] filesizew; // the size of the file received by each thread
  5. Public string strurl; // URL of the accepted File
  6. Public bool hb; // file merging mark
  7. Public int thread; // number of processes

Define an HttpFile class to manage the receiving thread. The Code is as follows:

 
 
  1. Public class HttpFile
  2. {
  3. Public Form1 formm;
  4. Public int threadh; // thread code
  5. Public string filename; // file name
  6. Public string strUrl; // the URL of the received File
  7. Public FileStream fs;
  8. Public HttpWebRequest re

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.