c# - 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}.");
}
}
解决方案
推荐阅读
- ios - 结合@Published 属性:在更新期间从其他地方获取当前值
- html - 难以理解如何删除一些填充
- node.js - 在客户端存储 JWT 令牌的位置以及如何保护它?
- r - 如何使用 R str_interp 或 str_glue 处理未分配的变量
- asp.net - 将 asp.net 代码推送到 azure devops 时出现 vsbuild 错误
- zip - 将文件夹存储到单个文件中,而不使用外部库或数据库
- ios - Swiftui Form View 跳转错误
- c# - 是否可以从外部渠道向微软团队发送消息
- python - 从哪里开始:允许用户通过比较对元素进行排名的 Python 脚本
- java - 如何在 Heroku 上公开 HTTPS 端口?