0

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
6
  • 1
    And you have a website/httpserver running that is listening on port 9000 on your localbox? If you run netstat -a you see a line with 9000 and LISTENING? Commented Sep 24, 2016 at 9:31
  • The tutorial does not say that does it, excuse my ignorance. It probably does. I assumed it was connecting to an existing service out there in cyberspace. So do you mean I need to do a prior tutorial first? Commented Sep 24, 2016 at 9:33
  • Yes, you need to have something running that accepts http connections. Commented Sep 24, 2016 at 9:35
  • Ok 9000 is not in the netstat thing. To quote you: ".. have something running ..." you mean I have to create my own CRUD web-service. As I said I assume it was looking for it on the web Commented Sep 24, 2016 at 9:37
  • 1
    Yep, you have to create that as well. That will be your HttpListener so your HttpClient can communicate with it. But it looks like you better follow docs.asp.net/en/latest/tutorials/first-web-api.html Commented Sep 24, 2016 at 9:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.