苏州网站设计营销公司,桐柏微网站建设,做网站网,国外服装设计网站我们知道用 WebRequest#xff08;HttpWebRequest、FtpWebRequest#xff09; 和 WebResponse#xff08;HttpWebResponse、FtpWebResponse#xff09;可以实现文件下载上传、网页抓取#xff0c;可是用 WebClient 更轻松。用 DownloadFile 下载网页using (System.Net.Web…我们知道用 WebRequestHttpWebRequest、FtpWebRequest 和 WebResponseHttpWebResponse、FtpWebResponse可以实现文件下载上传、网页抓取可是用 WebClient 更轻松。用 DownloadFile 下载网页using (System.Net.WebClient client new System.Net.WebClient()){ client.DownloadFile(http://www.cftea.com/, C:\\foo.txt);}就这样http://www.cftea.com/ 首页就被保存到 C 盘下了。用 DownloadData 或 OpenRead 抓取网页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);}我们将抓取来的网页赋给变量 str任由我们使用。也可以用 OpenRead 方法来获取数据流。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(); }}用 UploadFile 上传文件相对于 DownloadData、OpenReadWebClient 也具有 UploadData、OpenWrite 方法但最常用的恐怕还是上传文件也就是用方法 UploadFile。using (System.Net.WebClient client new System.Net.WebClient()){ client.Credentials new System.Net.NetworkCredential(用户名, 密码); client.UploadFile(ftp://www.cftea.com/foo.txt, C:\\foo.txt);}注意 UploadFile 的第一个参数要把上传后形成的文件名加上去也就是说这里不能是ftp://www.cftea.com/。用 UploadValues POST 数据WebClient wb new WebClient();NameValueCollection nvc new NameValueCollection();nvc.Add(param1, param1);nvc.Add(param2, param2);wb.UploadValues(url, post, nvc);转载于:https://www.cnblogs.com/jordan2009/archive/2012/10/18/2728888.html