Same question as: https://stackoverflow.com/questions/33671989/localhost-not-getting-resolved-from-c-sharp-code#=
Resource: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Code Snippet:
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/products/1"); //Fails Here
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync<Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
}
Do not understand answer given in the pre-mentioned question above:
The most likely explanation is that the code in question is blocked by a firewall and thus the connection is refused because the application doesn't have permission to reach the target. -- I opened my Firewall (Home computer) and saw my application in the list so I ticked the checkbox next to it, tried also combinations of Private or Public (have no idea what this is) however made no difference.
The next most likely explanation is that the target is listening only on IPv6 (not IPv4) and thus the connection fails when the DNS resolution returns the IPv4 address rather than the IPv6 address. -- ???
Error I get is:
An error occurred while sending the request.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:9000
netstat -ayou see a line with 9000 and LISTENING?