Search code examples
.net-coreconsole-applicationasp.net-core-webapiidentityserver4

How to fix "No such host is known" - at System.Net.Http.ConnectHelper.ConnectAsync


I'm developing a test project for learn to use IdentityServer 4, from an Udedemy's course. Now I have a console app that makes a call to the API that use IdentityServer authentication, at first I configure the HttpClient object with the Bearer token taken form the IdentityServer:

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");

Later I prepare an object for the POST method in the API for create a Customer and then I send throw the POT method like this:

                //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);
                }

At this moment everything looks fine but when I call the GetAsync method from the same HttpClient instance, for get all the customers from the same API. The console app throw this message in the exception "No such host is known."

There is the entire code:

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: I've tested all API's methods from Postman and everything looks fine, getting first the token for the authorization.

Thanks for your time, I appreciate any help. Greetings


Solution

  • The URL in this line seems a bit corrupt:

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