1.运行结果报“无法发送具有此谓词类型的内容正文的错误“,其实原因很简单,因为用的是GET的方式进行提交,而GetRequestStream()是用来在post提交的时候写post的内容的流,GET是不能有附加内容的,所以当然会报这个错。
1+. 远程服务器返回错误: (405) 不允许的方法。
405 - 用来访问本页面的 HTTP 谓词不被允许(方法不被允许)。
检查下POST和GET方法是否选择正确;
2.请求被中止: 未能创建 SSL/TLS 安全通道。
在WebRequest.Create前加入以下代码即可解决:
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
3.异常捕获。
借口内部的详细错误信息需要捕获后查看。
try{请求代码}
catch (WebException webEx)
{
var contentResponse2 = webEx.Response.GetResponseStream();
if (!contentResponse2.CanRead)
{
throw new EndOfStreamException("The stream cannot be read");
}
if (contentResponse2.CanSeek)
{
contentResponse2.Seek(0, 0);
}
var reader = new StreamReader(contentResponse2);
string content = reader.ReadToEnd();
throw;
}