Search code examples
c#httpclient

httpclient BaseAddress localhost Refers to webserver localhost or client localhost?


I have a webpage on a web server A with a line of code like this:

httpclient.BaseAddress = new Uri("http://localhost:64195/");

to call a Web API on my localhost.

As I want to make a webpage communicate with a client-side installed service/api/application, does it call the web server A machine localhost or client machine localhost? Thank you


Solution

  • Using an HttpClient to call localhost from the code behind your webforms page will make requests to the server machine. So, if you have another web app listening on port 64195, the following address is valid:

    httpclient.BaseAddress = new Uri("http://localhost:64195/");
    

    However, if you call the same address from a JavaScript file, the web page will make a request to the client machine, since localhost here refers to the address of whatever machine making the call.

    If you want to make a call from your JavaScript file to your other web app, consider using the domain name or some sort of proxy, e.g. fetch(example.com:64195/api/path)