string ip= SendRequest("http://pv.sohu.com/cityjson?ie=utf-8");
// <summary>
/// Get方式获取url地址输出内容
/// </summary> /// <param name="url">url</param>
/// <param name="encoding">返回内容编码方式,例如:Encoding.UTF8</param>
public static string SendRequest(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
return sr.ReadToEnd();
}
C# 获取公网IP
最新推荐文章于 2022-05-17 17:43:35 发布
该博客详细介绍了如何通过C#的HttpWebRequest类以GET方式请求并获取指定URL的响应内容。示例代码展示了如何创建请求,设置方法为GET,获取响应,并用StreamReader读取UTF8编码的响应流。此方法适用于网络爬虫或需要从网页抓取数据的场景。
2060

被折叠的 条评论
为什么被折叠?



