首页 > Windows开发 > 详细

C# 发送Http请求 - WebClient类

时间:2016-06-19 21:17:46      阅读:267      评论:0      收藏:0      [点我收藏+]

WebClient位于System.Net命名空间下,通过这个类可以方便的创建Http请求并获取返回内容。

一、用法1 - DownloadData

string uri = "http://hovertree.top/";

WebClient wc = new WebClient();

Console.WriteLine("Sending an HTTP GET request to " + uri);

byte[] bResponse = wc.DownloadData(uri);

string strResponse = Encoding.ASCII.GetString(bResponse);

Console.WriteLine("HTTP response is: ");

Console.WriteLine(strResponse);

// 何问起

二、用法2 - OpenRead 

string uri = "http://hovertree.net";

WebClient wc = new WebClient();

Console.WriteLine("Sending an HTTP GET request to " + uri);

Stream st = wc.OpenRead(uri);

StreamReader sr = new StreamReader(st);

string res = sr.ReadToEnd();

sr.Close();

st.Close();

Console.WriteLine("HTTP Response is ");

Console.WriteLine(res);
// 何问起

推荐:http://www.cnblogs.com/roucheng/p/3521864.html

C# 发送Http请求 - WebClient类

原文:http://www.cnblogs.com/roucheng/p/cshttp.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!