简体   繁体   中英

Authentication failed during request.GetResponse() in C# using HttpWebRequest

I am using C#, .Net framework 4.0 and trying to do HttpWebRequest for Http Basic Authentication and getting below error:


Inner Exception 1:
IOException: Authentication failed because the remote party has closed the transport stream.

I am using below code:


string svcCredentials = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", "Basic " + svcCredentials);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
    html = reader.ReadToEnd();
}

Error during GetResponse().

I tried all the references in SO:

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

Failure on HttpWebrequest with inner exception Authentication failed because the remote party has closed the transport stream

But of no use, still getting same error. Any problem in my code?

This issue is solved after changing ServicePointManager.SecurityProtocol as below:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;

As Tls 1.1, 1.2 are not available in .net framework 4.0 directly in System.Net.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM