我在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}.");
}
}