post请求url 远程服务器返回一个错误:401未经授权 255
publicstaticboolCheckValidationResult(objectsender,X509Certificatecertificate,X509Cha...
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
//直接确认,否则打不开
return true;
}
public static string PostHttp(string url, string body)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
string responseContent = string.Empty;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = 0;
httpWebRequest.Timeout = 20000;
byte[] btBodys = Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
HttpWebResponse httpWebResponse = null;
try
{ //下面一句开始报401
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return responseContent;
}
catch (WebException ex)
{
ServicePointManager.Expect100Continue = false;
httpWebResponse = (HttpWebResponse)ex.Response;
}
return responseContent;
} 展开
{
//直接确认,否则打不开
return true;
}
public static string PostHttp(string url, string body)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
string responseContent = string.Empty;
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = 0;
httpWebRequest.Timeout = 20000;
byte[] btBodys = Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
HttpWebResponse httpWebResponse = null;
try
{ //下面一句开始报401
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return responseContent;
}
catch (WebException ex)
{
ServicePointManager.Expect100Continue = false;
httpWebResponse = (HttpWebResponse)ex.Response;
}
return responseContent;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询