C # WebClient class usage instances

Source: Internet
Author: User

This article mainly introduces C # WebClient class usages, this article explains the use of WebClient download files, openwriter open a stream using the specified method to write data to the URI and upload file examples, the need for friends can refer to the following

In the project you want to implement a link page that enables you to call the specified project in Windows service. Because access to the page is using IE browser or other browsers, so remember to use the WebClient class.

If you only want to request a file from a specific URI, use WebClient, which is the simplest. NET class, which performs basic operations with only one or two commands, the. NET Framework currently supports URIs that begin with http:, HTTPS, and file: identifiers.

WebClient download file

There are two ways to use WebClient to download files, depending on how the contents of the file are handled, and if you want to save the file to disk only, use the DownloadFile () method, which has two parameters, that is, the requested URI and the data save location of the request file.

More often, the application needs to process the data retrieved from the Web site, using the OpenRead method, which returns a Stream object, which can then be extracted from the stream object into memory.

Example: OpenRead (string uri);

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21OpenRead (String uri)

#region read the HTML for the specified URI

///

Reads the HTML for the specified URI

///

///

///

private void Button4_Click (object sender, EventArgs e)

{

WebClient WC = new WebClient ();

String uri = "Http://127.0.0.1/rss/sina.aspx";

Stream stream = WC. OpenRead (URI);

StreamReader sr = new StreamReader (stream);

String strLine = "";

while (StrLine = Sr. ReadLine ())!= null)

{

THIS.LISTBOX1.ITEMS.ADD (StrLine);

}

Sr. Close ();

}

#endregion

Example: Openwriter (string uri,string method);

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19OpenWriter (String uri,string method)

#region to open a stream to write data to the URI using the specified method

///

Opens a stream to write data to the URI using the specified method

///

///

///

private void Button1_Click (object sender, EventArgs e)

{

WebClient WC = new WebClient ();

String uri = "Http://192.168.0.35/cims30/rss.txt";

Stream stream = WC. Openwrite (URI, "put");

StreamWriter sw = new StreamWriter (stream);

Sw. WriteLine ("Helloworldhelloworldhelloworldhelloworld");

Sw. Flush ();

Sw. Close ();

MessageBox.Show ("OK");

}

#endregion

The Openwriter method returns a writable stream of data that allows the user to send data to the URI, specifying the method by which the user sends the data to the host, which defaults to post, The example above assumes that 0.35 has a writable directory on the server, which is the code that creates the Rss.txt file in the directory, which reads "Helloworldhelloworldhelloworldhelloworld"

Uploading files

The WebClient class provides the UploadFile () and Uploaddata () methods that you can use when you need to post an HTML form or upload an entire file. The UploadFile () method uploads the file to the specified location, where the file name has been given, and the Uploaddata () methods upload the binary data provided by the byte array to the specified URI;

Example: Uploading Files

?

#region upload the local file to the specified URI

///

Upload the local file to the specified URI

///

///

///

private void Button2_Click (object sender, EventArgs e)

{

WebClient WC = new WebClient ();

String TargetPath = "Http://127.0.0.1/rss/Data configuration.zip";

String SourcePath = "D:data configuration.zip";

This.label1.Text = string. Format ("uploading {0} to {1}", TargetPath, SourcePath);

byte[] bt = WC. UploadFile (TargetPath, "put", sourcepath);

MessageBox.Show ("OK");

}

#endregion

#region upload the data buffer to the specified resource

///

Uploading data buffers to specified resources

///

///

///

private void Button3_Click (object sender, EventArgs e)

{

WebClient WC = new WebClient ();

String TargetPath = "Kaifeng.jpg";

String SourcePath = @ "C:test.jpg";

FileStream fs = new FileStream (SourcePath, FileMode.Open, FileAccess.Read);

byte[] bt = new Byte[fs. Length];

Fs. Read (BT, 0, Bt.) Length);

Wc. Uploaddata (TargetPath, "put", BT);

}

#endregion

The

WebClient feature is limited, especially the inability to use authentication certificates, so that the problem occurs when uploading data, and many sites do not accept unauthenticated upload files now. Although you can add header information to the request and check the corresponding header information, this is limited to the general meaning of the check, WebClient does not specifically support any of the protocols. This is because WebClient is a very generic class that can use any protocol to send requests and accept the corresponding, and it cannot handle any features that are specific to any protocol.

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.