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;
}
展开
 我来答
阳光的雷咩咩
2019-09-05 · TA获得超过1.4万个赞
知道大有可为答主
回答量:2.3万
采纳率:66%
帮助的人:7836万
展开全部
header呢?一般都要加一些身份验证在header里面。
怎么写你自己搜下
该写什么值建议你抓一下手动访问时候的包,或者看官方api说明
更多追问追答
追问
不懂,是什么意思
追答
那你要补充一下http的基本知识了。下载个fiddler或者wireshark捣鼓一下
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式