//初始化
private static void Init()
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
}
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
//为了通过证书验证,总是返回true
return true;
}
public static string HttpApi(string url, string jsonstr, string type)
{
Init();
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json";
request.Method = type.ToUpper().ToString();
byte[] buffer = encoding.GetBytes(jsonstr);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
http请求帮助类
最新推荐文章于 2023-05-27 23:20:54 发布