public static string DoPostByADK(string url, string jsonData,string token)
{
//发送Post请求
try
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", "Bearer " + token);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
Encoding encoding = Encoding.UTF8;//根据网站的编码自定义
string responseData = String.Empty;
if (Regex.IsMatch(url, "^https://"))
{
request.KeepAlive = false;
ServicePointManager.Expect100Continue = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
| SecurityProtocolType.Tls
| (SecurityProtocolType)0x300 //Tls11
| (SecurityProtocolType)0xC00 //Tls12
| (SecurityProtocolType)3072;
request.ProtocolVersion = HttpVersion.Version10;
}
if (jsonData != null)
{
var data = Encoding.UTF8.GetBytes(jsonData);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd();
}
return responseData;
}
}
catch (Exception ex)
{
throw ex;
}
}
标签:请求,request,ServicePointManager,发送,var,Post,data,SecurityProtocolType,string
From: https://www.cnblogs.com/Terrelll/p/17716536.html