Files
GeekDesk/Util/HttpUtil.cs

30 lines
1.1 KiB
C#
Raw Normal View History

using System.IO;
2021-07-12 17:28:57 +08:00
using System.Net;
using System.Text;
namespace GeekDesk.Util
{
public class HttpUtil
{
#region Get请求
2021-07-13 15:24:05 +08:00
public static string Get(string url)
2021-07-12 17:28:57 +08:00
{
2021-07-20 17:30:12 +08:00
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
2021-07-12 17:28:57 +08:00
//创建Web访问对 象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
2021-12-13 12:18:13 +08:00
myRequest.ContentType = "text/plain; charset=utf-8";
2021-07-12 17:28:57 +08:00
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
2021-07-12 17:28:57 +08:00
//通过响应内容流创建StreamReader对象因为StreamReader更高级更快
2021-12-13 12:18:13 +08:00
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
2021-07-12 17:28:57 +08:00
string returnStr = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
reader.Close();
myResponse.Close();
return returnStr;
}
#endregion
}
}