认证失败,因为远程方在从 webservice 获得响应时关闭了传输流异常

Authentication failed because the remote party has closed the transport stream exception when getting a response from webservice

我正在调用第三方服务,当我请求响应时,它抛出了一个异常

"Authentication failed because the remote party has closed the transport stream exception".

我认为发送凭据时出现问题。我什至尝试提供新的凭据。这是完整的代码

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();

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

我有一个限制,只能使用 TLS 1.2,而且最重要的是资源地址 (URL/address) 是本地的。所以上面的解决方案对我不起作用。在仔细分析 web.config 之后,我尝试使用 <bypasslist> 作为本地 URL,奇迹发生了!

<system.net>
    <defaultProxy>
      <proxy usesystemdefault="false" proxyaddress="<yourproxyaddress>" bypassonlocal="true" />
      <bypasslist>  
        <add address="[a-z]+\.abcd\.net\.au$" />  
      </bypasslist>
    </defaultProxy>
</system.net>

请注意,我已经在使用 <proxy> 设置来访问其他外部 URL,因此也不允许没有此设置。希望这对您有所帮助!

我有同样的问题,最初我更改了安全协议但它不起作用,然后我意识到我需要在创建 WebRequest 之前更改安全协议:

  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
        WebRequest request = WebRequest.Create(fullURL);
        request.Headers = requestHeaders;
        byte[] Bytes = System.Text.Encoding.ASCII.GetBytes(jsonData);