首页 > 解决方案 > Docker 容器内的 .NET Core 控制台应用程序 - System.Net.Http.CurlHandler.ThrowIfCURLEError(CURLcode 错误)

问题描述

我遇到了在 docker 容器上运行的 .Net 核心控制台应用程序 (ConsoleApp1) 的问题。我为 ConsoleApp1 设置了多个 DotNetcore 容器。我将环境变量与一个唯一的 URL 一起传递给每个容器。

我决定编写一个测试用例来测试为什么某些 URL 不起作用

我遇到的问题是一些容器能够调用客户端,但其他容器抱怨错误为:

ex = {System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't connect to server
   at System.Net.Http.CurlHandler.ThrowIfCURLEError(CURLcode error)
   at System.Net.Http.CurlHandler.MultiAge

这是我正在使用的代码:

public static async Task TestIfGivenURLPassORFail()
{
string _baseUrl = "https://abc.domain.dns.com";
//string _baseUrl = "https://def.domain.dns.com";
//string _baseUrl = "https://xyz.domain.dns.com";
string _username = "username";
string _password = "password";
string _extendedPath = "/xml/hoststatus?hostname=domain.mail.onmicrosoft.com";
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(_username, _password);
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
//using (handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true })
using (HttpClient client = new HttpClient(handler))
{
try
{
client.BaseAddress = new Uri(_baseUrl);
//client.DefaultRequestHeaders.Accept.Clear();
MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue("application/xml");
client.DefaultRequestHeaders.Accept.Add(contentType);
//Convert to XML
XmlDocument xmlDoc = new XmlDocument();
HttpResponseMessage response = await client.GetAsync(_extendedPath);
string stringData = await response.Content.ReadAsStringAsync();
xmlDoc.LoadXml(stringData);
if (stringData != null)
{
    Assert.True(true, "Test pass");
}
else
{
    Assert.True(false, "Test Fail");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while sending the request.  Response failed for {ex.StackTrace}.");
}
}

标签: c#dockercurlconsole-application

解决方案


推荐阅读