C # WebClient implement upload and download network resources

Source: Internet
Author: User

Download data

WebClient WC = new WebClient ();
1 string str= WC. Downloadstring ("Address");//download string directly

2 WC. DownloadFile ("Addredd", "fileName");//Download the file and specify the address to download to

3 byte[] B=WC. Downloaddata ("Address")//return a binary array directly and then convert

Wc. Dispose ();

Uploading data

String poststring = "arg1=a&arg2=b"; Here is the parameters passed, you can use the tool to grasp the packet analysis, but also to analyze themselves, mainly in the form of each name to add in byte[] postdata = Encoding.UTF8.GetBytes ( poststring); //coding, especially Chinese characters, in advance to see how to crawl the page encoding

WebClient WebClient = new WebClient ();

WEBCLIENT.HEADERS.ADD ("Content-type", "application/x-www-form-urlencoded"); //Take the header that must be added to the post, and if you change it to get it, remove the phrase byte[] responsedata = webclient.uploaddata ("url", "POST", postdata); Gets the return character stream string srcstring = Encoding.UTF8.GetString (responsedata); Decoding

C # WebClient implement upload and download network resources

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.