Я пытаюсь получить данные из метода APP функции Azure (этот URL-адрес Get API возвращает данные при прямом вводе в браузере), но не удается при попытке вызвать этот URL-адрес в моем коде C # со следующим внутренним исключением:
InnerException {"Невозможно прочитать данные из транспортного соединения: существующее соединение было принудительно закрыто удаленным хостом."} System.Exception {System.IO.IOException}
Ниже приведен код:
string html = string.Empty;
string url = @"https://mgyapi.azurewebsites.net/api/my_get/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url+ "421?code=xxxxxxxxxxwBuTCJpOIVmqBS8DIgE4MhTA==");
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Method = "GET";
//request.ContentType = contentType;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36";
request.CookieContainer = new CookieContainer();
//request.ContentLength = formData.Length;
request.Accept = "*/*";
request.Host = "mgyapi.azurewebsites.net";
request.KeepAlive = true;
request.Timeout = 999999999;
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //<----Here exception raises
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
Ниже приведен заголовок запроса, который я вижу в браузере:
Accept: text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Cache-Control: max-age=0
Connection: keep-alive
Host: mgyapi-v1.azurewebsites.net
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36