static class HttpTools
{
public static string Get(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
request.Method = WebRequestMethods.Http.Get;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
catch (WebException)
{
throw;
}
}
public static string Post(string url, string content, Encoding encoding, string contentType)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
request.Method = WebRequestMethods.Http.Post;
request.ContentType = contentType;
using (Stream stream = request.GetRequestStream())
{
byte[] data = encoding.GetBytes(content);
stream.Write(data, 0, data.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
catch (WebException)
{
throw;
}
}
}
HttpTools
于 2021-11-15 16:55:25 首次发布