I
I
Incold2020-02-27 14:44:42
ASP.NET
Incold, 2020-02-27 14:44:42

Why does System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a send.' occur?

Hello! I have a problem that I've been struggling with for a long time! I want to send an API request to the Guesty service (link to documentation: https://docs.guesty.com/#introduction) and get JSON.

string address = "https://api.guesty.com/api/v2/" + path + "?skip=" + sk + "&limit=100"; 
 string Storage = "*****" + storage + ".json";

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address);
webRequest.KeepAlive = false;
webRequest.ProtocolVersion = HttpVersion.Version11;
ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11; // здесь пробовал все возможные комбинации, в том числе  SecurityProtocolType.Tls и SecurityProtocolType.SystemDefault
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
webRequest.ContentLength = 0;
string autorization = extension.GuestyKeyAPI + ":" + extension.GuestySecret; //extension - это то, что приходит из полей формы
byte[] binaryAuthorization = Encoding.UTF8.GetBytes(autorization);
autorization = Convert.ToBase64String(binaryAuthorization);
autorization = "Basic " + autorization;
webRequest.Headers.Add("Authorization", autorization);
using (WebResponse response = (HttpWebResponse)webRequest.GetResponse()) // Здесь собственно и появляется сообщение об ошибке
{
     using (StreamReader reader = new StreamReader(response.GetResponseStream()))
     {
           using (StreamWriter writer = new StreamWriter(Storage))
           {
                 string s = reader.ReadToEnd();
                 writer.WriteLine(s);
                 reader.Close();
                 writer.Close();
            }
       }
}
string jsonString = File.ReadAllText(Storage);
return jsonString;


But the most annoying thing is not this, if you click on Continue in VS, then click on Submit again in the form, then everything suddenly works, JSON comes.
I also checked on another computer, the situation is the same there (all antiviruses are disabled).
Also tried to use HttpClient, but same error. How to solve this problem?
Thanks in advance for any help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
ofigenn, 2020-04-19
@ofigenn

If you go to another site, the error repeats?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question