. Net/C # supports resumable multi-thread download

Source: Internet
Author: User

I recently learned how to use c # to implement multi-threaded download, and finally let me google some materials. The following is a copy of the Code:
Namespace Microshaoft. Utils
{
Using System;
Using System. IO;
Using System. Net;
Using System. Text;
Using System. Security;
Using System. Threading;
Using System. Collections. Specialized;

/// <Summary>
/// Record the location of the downloaded byte
/// </Summary>
Public class DownLoadState
{
Private string _ FileName;

Private string _ AttachmentName;
Private int _ Position;
Private string _ RequestURL;
Private string _ ResponseURL;
Private int _ Length;

Private byte [] _ Data;

Public string FileName
{
Get
{
Return _ FileName;
}
}

Public int Position
{
Get
{
Return _ Position;
}
}

Public int Length
{
Get
{
Return _ Length;
}
}


Public string AttachmentName
{
Get
{
Return _ AttachmentName;
}
}

Public string RequestURL
{
Get
{
Return _ RequestURL;
}
}

Public string ResponseURL
{
Get
{
Return _ ResponseURL;
}
}


Public byte [] Data
{
Get
{
Return _ Data;
}
}

Internal DownLoadState (string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, byte [] Data)
{
This. _ FileName = FileName;
This. _ RequestURL = RequestURL;
This. _ ResponseURL = ResponseURL;
This. _ AttachmentName = AttachmentName;
This. _ Position = Position;
This. _ Data = Data;
This. _ Length = Length;
}

Internal DownLoadState (string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, ThreadCallbackHandler tch)
{
This. _ RequestURL = RequestURL;
This. _ ResponseURL = ResponseURL;
This. _ FileName = FileName;
This. _ AttachmentName = AttachmentName;
This. _ Position = Position;
This. _ Length = Length;
This. _ ThreadCallback = tch;
}

Internal DownLoadState (string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length)
{
This. _ RequestURL = RequestURL;
This. _ ResponseURL = ResponseURL;
This. _ FileName = FileName;
This. _ AttachmentName = AttachmentName;
This. _ Position = Position;
This. _ Length = Length;
}

Private ThreadCallbackHandler _ ThreadCallback;

Public HttpWebClient httpWebClient
{
Get
{
Return this. _ hwc;
}
Set
{
This. _ hwc = value;
}
}

Internal Thread thread
{
Get
{
Return _ thread;
}
Set
{
_ Thread = value;
}
}

Private HttpWebClient _ hwc;
Private Thread _ thread;

//
Internal void StartDownloadFileChunk ()
{
If (this. _ ThreadCallback! = Null)
{
This. _ ThreadCallback (this. _ RequestURL, this. _ FileName, this. _ Position, this. _ Length );
This. _ hwc. OnThreadProcess (this. _ thread );
}
}

}

// The signature of the method executed by the delegate proxy thread is consistent.
Public delegate void ThreadCallbackHandler (string S, string s, int I, int I );

// Exception Handling
Public enum ExceptionActions
{
Throw,
CancelAll,
Ignore,
Retry
}

/// <Summary>
/// Class containing Exception event data
/// </Summary>
Public class ExceptionEventArgs: System. EventArgs
{
Private System. Exception _ Exception;
Private ExceptionActions _ ExceptionAction;

Private DownLoadState _ DownloadState;

Public DownLoadState DownloadState
{
Get
{
Return _ DownloadState;
}
}

Public Exception
{
Get
{
Return _ Exception;
}
}

Public ExceptionActions ExceptionAction
{
Get
{
Return _ ExceptionAction;
}
Set
{
_ Predictionaction = value;
}
}

Internal ExceptionEventArgs (System. Exception e, DownLoadState DownloadState)
{
This. _ Exception = e;
This. _ DownloadState = DownloadState;
}
}

/// <Summary>
/// Class containing the DownLoad event data
/// </Summary>
Public class DownLoadEventArgs: System. EventArgs
{
Private DownLoadState _ DownloadState;

Public DownLoadState DownloadState
{
Get
{
Return _ DownloadState;
}
}

Public DownLoadEventArgs (DownLoadState DownloadState)
{
This. _ DownloadState = DownloadState;
}

}

Public class ThreadProcessEventArgs: System. EventArgs
{
Private Thread _ thread;

Public Thread thread
{
Get
{
Return this. _ thread;
}
}

Public ThreadProcessEventArgs (Thread thread)
{
This. _ thread = thread;
}

}

/// <Summary>
/// Supports resumable multi-thread download classes
/// </Summary>
Public class HttpWebClient
{
Private static object _ SyncLockObject = new object ();

Public delegate void DataReceiveEventHandler (HttpWebClient Sender, DownLoadEventArgs e );

Public event DataReceiveEventHandler DataReceive; // receives byte data events.

Public delegate void ExceptionEventHandler (HttpWebClient Sender, ExceptionEventArgs e );

Public event ExceptionEventHandler ExceptionOccurrs; // an exception occurs.

Public delegate void ThreadProcessEventHandler (HttpWebClient Sender, ThreadProcessEventArgs e );

Public event ThreadProcessEventHandler ThreadProcessEnd; // a multi-thread processing event occurs.


Private int _ FileLength; // The total size of the downloaded file

Public int FileLength
{
Get
{
Return _ FileLength;
}
}

/// <Summary>
/// Multipart download
/// </Summary>
/// <Param name = "Address"> URL </param>
/// <Param name = "FileName"> File name of the local path </param>
/// <Param name = "ChunksCount"> Number of blocks and threads </param>
Public void DownloadFile (string Address, string FileName, int ChunksCount)
{
Int p = 0; // position
Int s = 0; // chunk size
String a = null;
HttpWebRequest hwrq;
HttpWebResponse hwrp = null;
Try
{
Hwrq = (HttpWebRequest) WebRequest. Create (this. GetUri (Address ));
Hwrp = (HttpWebResponse) hwrq. GetResponse ();
Long L = hwrp. ContentLength;

Hwrq. Credentials = this. m_credentials;

L = (L =-1) | (L> 0x7fffffff ))? (Long) 0x7fffffff): L; // Int32.MaxValue the constant value is 2,147,483,647; that is, the hexadecimal 0x7FFFFFFF

Int l = (int) L;

This. _ FileLength = l;

// Reserve a space locally (you do not need to reserve a space in multi-thread mode)
// FileStream sw = new FileStream (FileName, FileMode. OpenOrCreate, FileAccess. ReadWrite, FileShare. ReadWrite );
// Sw. Write (new byte [l], 0, l );
// Sw. Close ();
// Sw = null;

Bool B = (hwrp. Headers ["Accept-Ranges"]! = Null & hwrp. Headers ["Accept-Ranges"] = "bytes ");
A = hwrp. Headers ["Content-Disposition"]; // attachment
If (! = Null)
{
A = a. Substring (a. LastIndexOf ("filename =") +

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.