如何修复 "No such host is known" - 在 System.Net.Http.ConnectHelper.ConnectAsync
How to fix "No such host is known" - at System.Net.Http.ConnectHelper.ConnectAsync
我正在开发一个测试项目来学习使用 IdentityServer 4,来自 Udedemy 的课程。现在我有一个控制台应用程序可以调用使用 IdentityServer 身份验证的 API,首先我使用从 IdentityServer 获取的 Bearer 令牌配置 HttpClient 对象:
namespace BankOfDotNet.ConsoleClient
{
class Program
{
private static HttpClient _client;
public static void Main(string[] args){
if (_client == null)
{
_client = new HttpClient();
}
MainAsync().GetAwaiter().GetResult();
}
private static async Task MainAsync()
{
try
{
//Discover all endpoint using metadata og identity sever
var client = new HttpClient();
var apiUri = "http://localhost:5000";
var disco = await _client.GetDiscoveryDocumentAsync(apiUri);
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
//Grab a bearer token
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "client",
ClientSecret = "secret",
Scope = "bankOfDotNetApi.read",
});
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
稍后我为 API 中的 POST 方法准备了一个对象来创建一个 Customer 然后我像这样发送抛出 POT 方法:
//Consume Customer API
_client.SetBearerToken(tokenResponse.AccessToken);
var customerInfo = new StringContent(
JsonConvert.SerializeObject(new
{
Id = 6,
FirstName = "Dariel",
LastName = "Amores"
}), Encoding.UTF8, "application/json");
var createCustomerResponse = await _client.PostAsync("http://localhost:16181/api/customers", customerInfo);
if (!createCustomerResponse.IsSuccessStatusCode)
{
Console.WriteLine(createCustomerResponse.StatusCode);
}
此时一切看起来都很好,但是当我从同一个 HttpClient 实例调用 GetAsync 方法时,为了从同一个 API 获取所有客户。控制台应用程序在异常“没有这样的主机是已知的”中抛出此消息。
这里有完整的代码:
using IdentityModel.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace BankOfDotNet.ConsoleClient
{
class Program
{
private static HttpClient _client;
public static void Main(string[] args){
if (_client == null)
{
_client = new HttpClient();
}
MainAsync().GetAwaiter().GetResult();
}
private static async Task MainAsync()
{
try
{
//Discover all endpoint using metadata og identity sever
var client = new HttpClient();
var apiUri = "http://localhost:5000";
var disco = await _client.GetDiscoveryDocumentAsync(apiUri);
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
//Grab a bearer token
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "client",
ClientSecret = "secret",
Scope = "bankOfDotNetApi.read",
});
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
//Consume Customer API
_client.SetBearerToken(tokenResponse.AccessToken);
var customerInfo = new StringContent(
JsonConvert.SerializeObject(new
{
Id = 6,
FirstName = "Dariel",
LastName = "Amores"
}), Encoding.UTF8, "application/json");
var createCustomerResponse = await _client.PostAsync("http://localhost:16181/api/customers", customerInfo);
if (!createCustomerResponse.IsSuccessStatusCode)
{
Console.WriteLine(createCustomerResponse.StatusCode);
}
var getCustomerResponse = await _client.GetAsync("http://http://localhost:16181/api/customers");
if (!getCustomerResponse.IsSuccessStatusCode)
{
Console.WriteLine(getCustomerResponse.StatusCode);
}
else
{
var content = await getCustomerResponse.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
P.S:我已经测试了 Postman 的所有 API 方法,一切看起来都很好,首先获得授权令牌。
感谢您的宝贵时间,感谢您的帮助。问候
这一行中的 URL 似乎有点损坏:
var getCustomerResponse = await _client.GetAsync("http://http://localhost:16181/api/customers");
我正在开发一个测试项目来学习使用 IdentityServer 4,来自 Udedemy 的课程。现在我有一个控制台应用程序可以调用使用 IdentityServer 身份验证的 API,首先我使用从 IdentityServer 获取的 Bearer 令牌配置 HttpClient 对象:
namespace BankOfDotNet.ConsoleClient
{
class Program
{
private static HttpClient _client;
public static void Main(string[] args){
if (_client == null)
{
_client = new HttpClient();
}
MainAsync().GetAwaiter().GetResult();
}
private static async Task MainAsync()
{
try
{
//Discover all endpoint using metadata og identity sever
var client = new HttpClient();
var apiUri = "http://localhost:5000";
var disco = await _client.GetDiscoveryDocumentAsync(apiUri);
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
//Grab a bearer token
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "client",
ClientSecret = "secret",
Scope = "bankOfDotNetApi.read",
});
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
稍后我为 API 中的 POST 方法准备了一个对象来创建一个 Customer 然后我像这样发送抛出 POT 方法:
//Consume Customer API
_client.SetBearerToken(tokenResponse.AccessToken);
var customerInfo = new StringContent(
JsonConvert.SerializeObject(new
{
Id = 6,
FirstName = "Dariel",
LastName = "Amores"
}), Encoding.UTF8, "application/json");
var createCustomerResponse = await _client.PostAsync("http://localhost:16181/api/customers", customerInfo);
if (!createCustomerResponse.IsSuccessStatusCode)
{
Console.WriteLine(createCustomerResponse.StatusCode);
}
此时一切看起来都很好,但是当我从同一个 HttpClient 实例调用 GetAsync 方法时,为了从同一个 API 获取所有客户。控制台应用程序在异常“没有这样的主机是已知的”中抛出此消息。
这里有完整的代码:
using IdentityModel.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace BankOfDotNet.ConsoleClient
{
class Program
{
private static HttpClient _client;
public static void Main(string[] args){
if (_client == null)
{
_client = new HttpClient();
}
MainAsync().GetAwaiter().GetResult();
}
private static async Task MainAsync()
{
try
{
//Discover all endpoint using metadata og identity sever
var client = new HttpClient();
var apiUri = "http://localhost:5000";
var disco = await _client.GetDiscoveryDocumentAsync(apiUri);
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
//Grab a bearer token
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "client",
ClientSecret = "secret",
Scope = "bankOfDotNetApi.read",
});
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
//Consume Customer API
_client.SetBearerToken(tokenResponse.AccessToken);
var customerInfo = new StringContent(
JsonConvert.SerializeObject(new
{
Id = 6,
FirstName = "Dariel",
LastName = "Amores"
}), Encoding.UTF8, "application/json");
var createCustomerResponse = await _client.PostAsync("http://localhost:16181/api/customers", customerInfo);
if (!createCustomerResponse.IsSuccessStatusCode)
{
Console.WriteLine(createCustomerResponse.StatusCode);
}
var getCustomerResponse = await _client.GetAsync("http://http://localhost:16181/api/customers");
if (!getCustomerResponse.IsSuccessStatusCode)
{
Console.WriteLine(getCustomerResponse.StatusCode);
}
else
{
var content = await getCustomerResponse.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
P.S:我已经测试了 Postman 的所有 API 方法,一切看起来都很好,首先获得授权令牌。
感谢您的宝贵时间,感谢您的帮助。问候
这一行中的 URL 似乎有点损坏:
var getCustomerResponse = await _client.GetAsync("http://http://localhost:16181/api/customers");