C # Authentication failed because the remote side closed the transport stream

I want to note that I searched a lot for this without a solution. So, I created a loop that will start listBox2 containing links, each time creating an HTTP GET request to access the full source code.

My code is:

private void button4_Click(object sender, EventArgs e) { try { for (int i = 0; i < listBox2.Items.Count; i++) { code(listBox2.Items[i].ToString() + "\'"); //await PutTaskDelay(); //Console.WriteLine(listBox2.Items[i].ToString()); if (VarrileKeeper.s.ToLower().Contains("mysql")) { Console.WriteLine("possitive " + listBox2.Items[i].ToString()); } } } catch (Exception Fejl) { Console.WriteLine(Fejl); } } public static String code(string Url) { System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url); myRequest.MaximumAutomaticRedirections = 4; myRequest.MaximumResponseHeadersLength = 4; myRequest.Credentials = CredentialCache.DefaultCredentials; myRequest.Method = "GET"; WebResponse myResponse = myRequest.GetResponse(); StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8); string result = sr.ReadToEnd(); sr.Close(); myResponse.Dispose(); myResponse.Close(); VarrileKeeper.s = result; return result; } 

The error below is triggered when the loop accesses various URLs, for example ( http://www.memphremagog.org/fr/lexique.php?id=32 ). The strange thing is that if I delete the loop and do an HTTP GET reqeust on this site, everything will work fine.

The first exception of type "System.Net.WebException" exception occurred in System.dll System.Net.WebException: the underlying connection was closed: an unexpected error occurred while sending. ---> System.IO.IOException: Authentication failed because the remote side has closed the transport stream.

I don’t think this is a problem with the certificate because otherwise I still get the error message http://www.memphremagog.org/fr/lexique.php?id=32 when I delete the loop.

Any suggestions are welcome.

+11
source share
2 answers

The url zvoxaudio.com, from your comments, is pretty complicated. They have some bot detection for adding items to the basket. The link you are using does not seem to work regardless of the fact that, I think, it uses an id with an expired number; I was able to use it by updating id to 4007001 and changing it to "https". However, this link adds an item to the cart, and the site does not allow bots to add items to the cart, the site returns an error 400 with this key: value in the headers:

 X-Error-Message: Bots are not allowed to add items to cart 

Try adding UserAgent to your query as follows:

  myRequest.UserAgent = "Nada"; 

The ZVOXAUDIO link works with these small changes!

If you want to save the URL as an "https" request, just add it to your code.

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 

This is required because ZVOXAUDIO does not support TLS 1.0 or earlier. If the .NET version you are using does not support TLS 1.2, you can use SecurityProtocolType.Tls11, because ZVOXAUDIO still supports TLS 1.1.

But since, apparently, you are trying to run this launch on as many sites as possible, you probably do not want to allow only TLS 1.2, as older servers may not support it. I would set the security protocol as follows:

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

As a suggestion, you can set the server certificate verification callback and the security protocol outside of your loop, since it is a static parameter for all requests. Also, if you use C # syntax, the Dispose method takes care of closing and deleting the WebResponse and StreamReader variables for you. And C # introduced "var" back in .NET 3.0, you might want to start hugging it, assuming you're using 3.0 or a newer structure. If you want to see how it will look, look below.

Make sure you have these operations:

 using System; using System.IO; using System.Net; 

Then put these two lines in the static constructor of your form or object, I assume that you are using the form right now:

 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; 

Then your two methods will look like this:

 private void button4_Click(object sender, EventArgs e) { try { for (var i = 0; i < listBox2.Items.Count; i++) { var response = Code(listBox2.Items[i].ToString() + "\'"); if (response.ToLower().Contains("mysql")) { Console.WriteLine("positive " + listBox2.Items[i].ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static string Code(string url) { var webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.MaximumAutomaticRedirections = 4; webRequest.MaximumResponseHeadersLength = 4; webRequest.UserAgent = "Mozilla/5.0 (Taco2) Gecko/20100101"; webRequest.Credentials = CredentialCache.DefaultCredentials; webRequest.Method = "GET"; using (var webResponse = webRequest.GetResponse()) { using (var sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8)) { return sr.ReadToEnd(); } } } 

Happy coding! Feel free to ask any questions in the comments below.

+14
source

This happened to me, but for me it was related to a security protocol issue.

Just add this line

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

More info β†’ https://codeshare.co.uk/blog/how-to-fix-the-error-authentication-failed-because-the-remote-party-has-closed-the-transport-stream/

0
source

Source: https://habr.com/ru/post/1265904/


All Articles