C#使用WebClient获取给定地址的内容(POST方式传参)

见下方代码:

 1 string url = "https://www.baidu.com";//源地址
 2 System.Net.WebClient WebClient = new System.Net.WebClient();
 3 var parameter = "x=7&y=8&z=9";//参数
 4 byte[] parameters = Encoding.UTF8.GetBytes(parameter);//UTF8编码
 5 WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//post方式传参不可或缺的一句代码
 6 byte[] responseData = WebClient.UploadData(url, "post", parameters);//得到返回的内容
 7 String str = Encoding.UTF8.GetString(responseData);//得到的目的字符串
 8 //DownloadString(url)也可以实现该功能但是不能传参。
 9 //WebClient.Encoding = System.Text.Encoding.UTF8;
 10 //String str = WebClient.DownloadString(url);

 

posted on 2015-04-24 17:46  sukra  阅读(1243)  评论(0)    收藏  举报

导航