身份验证失败,因为远程方在从Web服务获取响应时已关闭传输流异常

Chi*_*g K 43 c# web-services windows-authentication httpwebresponse

我正在调用第三方服务,当我要求回复时,它会抛出一个异常

"身份验证失败,因为远程方已关闭传输流异常".

我认为发送凭据存在问题.我甚至尝试过提供新的凭据.这是完整的代码

string get_url = "https://**.*******.com/com/******/webservices/public_webservice.cfc?wsdl&Method=CreateUser&SiteID=**&WSPassword=******&UserName=******";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(get_url);
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Credentials = CredentialCache.DefaultCredentials;
//request.UseDefaultCredentials = false;
//request.Credentials = new System.Net.NetworkCredential("*****", "*****");
request.ContentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";

// Show the sent stream
//lbl_send_stream.Text = send_stream;
//lbl_send_stream.Text = get_url;
// Get UserId And LoginToken From Third Party DB
// ==============================================
//Exception gets throwed When code hits here
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Chi*_*g K 101

我找到了答案,这是因为我们调用的第三方Web服务不支持TLS 1.0,它们支持1.1和1.2.所以我不得不改变安全协议.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Run Code Online (Sandbox Code Playgroud)

  • 如果使用.Net 4.0,则可以使用TLS1.2的数值`ServicePointManager.SecurityProtocol =(SecurityProtocolType)3072; (9认同)
  • 您可以在从请求获取HTTPResponse之前添加它. (5认同)
  • 另请参阅https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/了解其他选项和框架。 (2认同)