使用WebClient 获得网页内容或提交请求

WebClient 提供的向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法。

使用时需要将捕捉到的Http头部放入Headers中。(捕获http可以使用HttpWatch)

WebClient wc = new WebClient();
wc.Credentials
= new NetworkCredential(username, password); //对于需要登录的网站设置验证信息
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string content = wc.DownloadString(url); // 获得内容
string postString = "name=peter&age=24";
byte[] postData = Encoding.ASCII.GetBytes(postString);
byte[] responseArray = wc.UploadData(url, postData);
// 提交数据

 

 

 

posted on 2011-09-17 12:33  PeterZhang  阅读(1159)  评论(0编辑  收藏  举报