System.Net.Http.HttpRequestException: Unable to connect to the remote server
Однако:
- Тот же API отлично работает в моем локальном среде.
- Вызов API также работает в Postman с точно такими же деталями запроса.< /p>
Проверено Конечная точка API:
- Протестировал API в Postman и в моей локальной среде, он работает отлично.
- Ожидается: API будет работать аналогично в рабочей среде.
- Результат: сбой с невозможностью подключения к удаленный сервер.
{
var data = "";
var logs = new List();
try
{
logs.Add("API call started.");
// Security protocol configuration
System.Net.ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;
// Base URL from configuration
var baseUrl = "https://api.example.com/";
HttpClient client = new HttpClient { BaseAddress = new Uri(baseUrl) };
client.Timeout = TimeSpan.FromMinutes(5);
// Construct the final URL
var apiUrl = $"readmeter?key={apiKey}&cnt=10000&format=json&timezone=America~New_York&devices={meter}&start_date={fromDate}&end_date={toDate}";
logs.Add($"API URL: {client.BaseAddress}{apiUrl}");
// Make the API call
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
logs.Add("API call successful.");
string resultContent = await response.Content.ReadAsStringAsync();
// Process response here...
}
else
{
logs.Add($"API failed. Status code: {response.StatusCode}");
}
}
catch (Exception ex)
{
logs.Add($"Error: {ex.Message}");
if (ex.InnerException != null)
{
logs.Add($"Inner Exception: {ex.InnerException.Message}");
}
}
return Json(new { data, logs }, JsonRequestBehavior.AllowGet);
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... rks-into-t