【问题标题】:How to fix "No such host is known" - at System.Net.Http.ConnectHelper.ConnectAsync如何修复“不知道这样的主机” - 在 System.Net.Http.ConnectHelper.ConnectAsync
【发布时间】:2021-06-16 10:37:04
【问题描述】:

我正在开发一个测试项目,用于从 Udedemy 的课程中​​学习使用 IdentityServer 4。现在我有一个控制台应用程序,它调用使用 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 方法准备一个对象以创建客户,然后我发送抛出 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 获取所有客户。控制台应用程序在异常“No such host is known”中抛出此消息。

有完整的代码:

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 方法,一切看起来都很好,首先获得了授权令牌。

感谢您的宝贵时间,感谢您的帮助。问候

【问题讨论】:

    标签: .net-core console-application asp.net-core-webapi identityserver4


    【解决方案1】:

    此行中的 URL 似乎有点损坏:

    var getCustomerResponse = await _client.GetAsync("http://http://localhost:16181/api/customers");
    
    
               
            
    

    【讨论】:

    • 谢谢大佬 已经深夜了!!!你让我免于痛苦
    • 没错,但我没有足够的声望来投票给你的答案。
    猜你喜欢
    • 2021-11-17
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多