Multithreading File transfer protocol HTTP using C # under - reprint

Source: http://www.jztop.com/dev/C/2006-07-05/22373.html

  many people have had to use the network ants Network Express software or download files Internet experience, the use of these software can greatly speed up Internet file transfer speed, reduce file transfer time. Why are these software have so much magic it? The main reason is that these software have adopted a multi-threaded downloads and HTTP technology. If we own to write a program like this, it is possible to quickly download files on the Internet, it must be a very pleasant thing. Here I will talk about how to use the C # language support multi-threaded download a program file, you will see the network using the C # language program should be how easy it is, from which we can understand C # language in a powerful network functions.

  First, tell us about the HTTP protocol, HTTP acronym that is Hpyer Text Transfer Protocal, which is the most important modern Internet network protocol, hypertext transfer protocol between the application layer TCP / IP protocol is a connectionless, simple, fast protocol C / S structure. HTTP connection points of the working process substantially, request, response, and disconnect the four steps. C # language on the HTTP protocol to provide good support, provides the WebRequest and WebResponse classes in the .NET class library, these two categories are included in the System.Net namespace, using the two class can implement many advanced networking features herein is the use of multi-threaded file download these two classes. WebRequest and WebResponse are abstract base class, and therefore can not be directly, to be inherited as an object used in the program, the actual use, the choice of the appropriate subclasses thereof The prefix URI parameter in the URI, such URI for the HTTP, and the HttpWebRequest HttpWebResponse class can be used to handle clients with WEB server HTTP communications between.

  HttpWebRequest class implements a lot of access via HTTP WEB Server advanced features on the file. HttpWebRequest class provides support for the properties and methods WebRequest defined, HttpWebRequest sent to the Internet resource value of the common HTTP headers as attributes, provided by the method or system, conventional HTTP header by properties or methods of: receiving , by the Accept property, is connected, by the connection KeepAlive property and property, ContentLength,, ContentType, the ContentType property, the range set by the property set by the ContentLength AddRange methods the actual use of the header information is correct Once set, delivered to the WEB server , WEB server to respond to the request.

  HttpWebResponse class inherits from the WebResponse class to deal specifically from WEB server HTTP response returned, this class implements many ways, has many properties that can deal comprehensively with Internet information received. In HttpWebResponse class, for most common HTTP header fields, has a separate attribute corresponding thereto, the programmer may be located conveniently HTTP access by the attribute information field in the packet header received message, the present embodiment used in HttpWebResponse class attribute: ContentLength receive both content length.

  Once you have the above understanding, take a look at the following two classes of usage, to create HttpWebRequest objects, do not directly use the HttpWebRequest constructor, and the method to be used to initialize a WebRequest.Create HttpWebRequest instance, such as:

HttpWebRequest hwr=(HttpWebRequest)WebRequest.Create(http://www.163.com/);

  After creating an object, it can HttpWebRequest property, provided many content of the HTTP header fields, such as hwr.AddRange (100,1000); the received set target range from 100 to 1000 bytes.

  When HttpWebReques object using GetResponse () method returns a HttpWebResponse object is proposed HTTP return message information, the need to use the GetResponseStream HttpWebResponse () method returns a Stream object, HTTP return message can be read, for example: first, a definition of the object Strean public System.IO.Stream ns; and ns = hwr.GetResponse () .GetResponseStream () ; Stream object to create. Once you have more than ready to start designing knowledge under our multi-thread download Internet files, first open Visual Studio.Net Integrated Development Environment, select "File", "New", "Project", then select "Visual C # project." , select the wizard on the right list box "Windows application", enter a project name, as in this example: httpftp, then select the "OK" button, the wizard automatically generates a Windows application project. First, open the window design design application window, add the following controls:

  a list box listBox1 three text labels label1-label3 three text boxes textBox1-textBox3 receiving a start button button1 designed window below:

47e02449f4f03ea887b3c9210f55bceb.gif

  Control custom 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;

  The code editor opens Form1, add the following namespace:

using System.Net; // network functions
using System.IO; // streaming support
using System.Threading; // thread support

  Increase program variables as follows:

public bool [] threadw; // each thread marks the end of
public string [] filenamew; // each thread receives the file name of the file
public int [] filestartw; // each thread starting position to receive the file
public int [] filesizew; // file size of each thread receives
public string strurl; URL // accept the document
public bool hb; // file merge flag
public int thread; // number of processes

Reproduced in: https: //www.cnblogs.com/llbofchina/archive/2006/07/22/457491.html

Guess you like

Origin blog.csdn.net/weixin_34221276/article/details/94206713