docker container raise socket error when request with HttpClient Cannot assign requested address (localhost:8085)

Viewed 21

I have a docker container that normally I can request with postman, but when I send a request with the my local restapi, it gives an error.

System.Net.Http.HttpRequestException: Cannot assign requested address (localhost:8085) ---> System.Net.Sockets.SocketException (99): Cannot assign requested address at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request) at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)

My local restapi code is :

        private const string URL = "http://localhost:8085/Patient";
        private string urlParameters = "f51e3471-fd3b-4b64-87f4-1fab90c8e1cd";
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);
        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = await client.GetAsync(urlParameters); 

my postman request is :

{ "info": { "_postman_id": "91631d48-83ca-4cb8-b697-ba6be688d544", "name": "New Collection", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", "_exporter_id": "7001078" }, "item": [ { "name": "Get Patient 1", "protocolProfileBehavior": { "disableBodyPruning": true }, "request": { "method": "GET", "header": [], "body": { "mode": "raw", "raw": "", "options": { "raw": { "language": "json" } } }, "url": { "raw": "http://localhost:8085/Patient/f51e3471-fd3b-4b64-87f4-1fab90c8e1cd1", "protocol": "http", "host": [ "localhost" ], "port": "8085", "path": [ "Patient", "f51e3471-fd3b-4b64-87f4-1fab90c8e1cd1" ] } }, "response": [] } ] }

0 Answers
Related