WebClient allows you to easily download and upload files and capture webpages.

Source: Internet
Author: User
We know that webrequest (httpwebrequest, ftpwebrequest) and webresponse (httpwebresponse, ftpwebresponse) can be used to download and upload files and capture webpages, but WebClient is easier to use. Download the web page using (system. net. webClient client = new system. net. webClient () {client. downloadfile ("http://www.cftea.com/", "C: \ foo.txt");} in this way, the http://www.cftea.com/home page is saved to the C disk. Use downloaddata or openread to capture the web page using (system. net. webClient client = new system. net. webClient () {byte [] bytes = client. downloaddata ("http://www.cftea.com/"); string STR = (system. text. encoding. getencoding ("gb2312 "). getstring (bytes);} We assigned the crawled webpage to the variable STR, which is used by us. You can also use the openread method to obtain data streams. Using (system. net. webClient client = new system. net. webClient () {using (system. io. stream stream = client. openread ("http://www.cftea.com/") {using (system. io. streamreader reader = new system. io. streamreader (stream, system. text. encoding. getencoding ("gb2312") {string STR = reader. readtoend (); reader. close ();} stream. close () ;}use uploadfile to upload files. Compared with downloaddata and openread, WebClient also has uploaddata, Openwrite method, but the most common method is to upload files, that is, using the uploadfile method. Using (system. net. webClient client = new system. net. webClient () {client. credentials = new system. net. networkcredential ("username", "password"); client. uploadfile ("ftp://www.cftea.com/foo.txt", "C: \ foo.txt");} pay attention to the first parameter of uploadfile, add the file name after upload, that is, it cannot be: ftp://www.cftea.com /.

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.