1.引用组件
using System.Net; using
System.IO;
2.下载数据
一:DownloadFile
System.Net.WebClient wc = new System.Net.WebClient();
client.DownloadFile("http://www.baidu.com/", "E:\\baidu.txt");
二:DownloadData
System.Net.WebClient wc = new System.Net.WebClient();
byte[] bytes = wc.DownloadData("http://www.baidu.com/");
//百度编码是GB2312
string str = (System.Text.Encoding.GetEncoding("gb2312").GetString(bytes);
三:OpenRead
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream stream = wc.OpenRead("http://www.baidu.com/");
System.IO.StreamReader reader = new System.IO.StreamReader
(stream, System.Text.Encoding.GetEncoding("gb2312")))
string str = reader.ReadToEnd();
reader.Close();
stream.Close();
2.上传数据
一:UploadFile
System.Net.WebClient wc = new System.Net.WebClient();
wc.UploadFile("http://localhost/uploadFile.aspxs/a.txt", "E:\\a.txt");
二:UploadData
System.Net.WebClient wc = new System.Net.WebClient();
string postData ="wc=webclient";
wc.Headers.Add("Content-Type","application/x-www-form-urlencoded");
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
//上传并获取返回数据
byte[] responseArray = wc.UploadData(http://localhost/uploadFile.aspxs/a.txt","POST",byteArray);