Answer the question
In order to leave comments, you need to log in
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;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question