"Reference" WebClient download data

Source: Internet
Author: User

Net2.0 a lot of new components, WebClient is one, the function is also very powerful, today take WebClient do a small experiment, only use some very simple function can realize the function of the previous bad realization, very convenient.

A brief introduction to WebClient:
The WebClient class provides public methods for sending data to and receiving data from any local, Intranet, or Internet resource identified by the URI.
The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data through any WebRequest descendants that have been registered with the WebRequest.RegisterPrefix method.
Attention
By default, the. NET Framework supports URIs that begin with http:, https:, ftp:, and file: scheme identifiers.

     The following describes the WebClient method for uploading data to a resource:  
    openwrite    retrieves a Stream that is used to send data to a resource.
    OpenWriteAsync   retrieves a Stream that sends data to a resource without blocking the calling thread.
    UploadData   sends a byte array to the resource and returns a byte array that contains any responses.
    UploadDataAsync   sends a Byte array to a resource without blocking the calling thread.
    UploadFile   sends a local file to the resource and returns a Byte array that contains any responses.
    UploadFileAsync   sends a local file to a resource without blocking the calling thread.
    UploadValues   sends the NameValueCollection to the resource and returns a Byte array that contains any responses.
    UploadValuesAsync   sends NameValueCollection to a resource without blocking the calling thread, and returns a Byte containing any response Array.
    UploadString   sends a String to a resource without blocking the calling thread.
    UploadStringAsync   sends a String to a resource without blocking the calling thread.  

     The following describes the WebClient method for downloading data from a resource:  
    openread    returns data as a Stream from a resource.
    OpenReadAsync   returns data from a resource without blocking the calling thread.
    DownloadData  downloads data from a resource and returns a Byte array.
    DownloadDataAsync   downloads data from a resource and returns a Byte array without blocking the calling thread.
    DownloadFile   Download data from the resource to a local file.
    DownloadFileAsync   downloads data from a resource to a local file without blocking the calling thread.
    DownloadString   Downloads A string from a resource and returns a string.
    DownloadStringAsync   Downloads A String from a resource without blocking the calling thread.  

You can use the CancelAsync method to cancel an asynchronous operation that has not yet completed.
By default, the WebClient instance does not send an optional HTTP header. If your request requires an optional header, you must add the header to the Headers collection. For example, to persist a query in a response, you must add the user Agent header. Additionally, if the user agent header is lost, the server may return 500 (internal server error).
In the WebClient instance, AllowAutoRedirect is set to true.
Note to inheritors derived classes should invoke the base class implementation of WebClient to ensure that derived classes work as expected.

To achieve the source code:

1. Using System;

2. Using System.Collections.Generic;

3. Using System.ComponentModel;

4. Using System.Data;

5. Using System.Drawing;

6. Using System.Text;

7. Using System.Windows.Forms;

8. Using System.Net;

9. Using System.IO;

10.

Namespace Wiindowsformsapplication

12. {

public partial class Form1:form

14. {

Public Form1 ()

16. {

InitializeComponent ();

This.textBox1.Text = @ "Http://dl-sh-ocn-1.pchome.net/0d/bx/koomail50b8.rar";

19.}

20.

WebClient WebClient = new WebClient ();

private void Btn_down_click (object sender, EventArgs e)

23. {

24.

if (webclient.isbusy)//whether there is a Web request in progress

26. {

Webclient.cancelasync ();

28.}

29.//Add event for WebClient

Webclient.downloadprogresschanged +=new Downloadprogresschangedeventhandler (webclient_downloadprogresscha nged);

Webclient.downloadfilecompleted+=new Asynccompletedeventhandler (webclient_downloadfilecompleted);

32.//Start Download

Webclient.downloadfileasync (New Uri (This.textBox1.Text), "Aa.rar");

34.}

35.

webclient_downloadprogresschanged private void (object sender, DownloadProgressChangedEventArgs e)

37. {

This.progressBar1.Value = E.progresspercentage;

This.lbl_pro. Text = e.progresspercentage.tostring () + "%";

This.lbl_detail. Text = string. Format ("Downloading file, completing progress {0}/{1} (bytes)"

E.bytesreceived.,

, e.totalbytestoreceive);

43.}

44.

private void Webclient_downloadfilecompleted (object sender, AsyncCompletedEventArgs e)

46. {

if (e.cancelled)

MessageBox.Show ("The download was canceled! ");

. else

MessageBox.Show ("Download done! ");

51.}

52.

Btn_cancel_click private void (object sender, EventArgs e)

54. {

This.webClient.CancelAsync ();

This.webClient.Dispose ();

57.}

58.}

59.}

Operating interface:

"Reference" WebClient download data

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.