Night Monger Night Monger - 3 years ago 123
HTTP Question

No connection could be made because the target machine actively refused

Hold on. before you mark this as duplicate. I am having a slightly different issue.
So I'm calling an api that throws this exception. But - the catch is This happens only when the call is made from server. If i make an angular /ajax call - this works perfectly fine.
Where as in .net when I write the following

using (HttpClient hc = new HttpClient())
{
hc.BaseAddress = new Uri(endpoint);
hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = hc.GetAsync(url).Result;
}


I get the inner exception saying No connection could be made because the target machine actively refused it xx.xxx.xx.xx:pp

I checked all the links Here and here. They all are talking about Firewall / Network issues. I dont feel this is firewall or network because it works via ajax.
I am thinking more to do with Server configuration. Any pointers would be helpful.

Also - this works when I make the call from browser or via fiddler.

Answer Source

So i finally i figured what the reason was - but i am not sure why it happens.

The reason is because - Visual studio was not honoring our proxy settings. Essentially - we are talking about 3 servers here

So my machine ip : xxx.xx.xx.1
Our Company Proxy server ip : xx.xx.xx.2
The api ip : xxx.xx.xx.3 url(api.com)

Every call is routed through our proxy that i can see in my internet settings. So when i installed wireshark and saw the route --> When i access from visual studio it directly tries to access api server from my host machine without going via the proxy.

that is: xxx.xx.xx.1 --> xxx.xx.xx.3 [Failed]

Where as when i try from browser - it honors the proxy setting

that is : xxx.xx.xx.1 --> xxx.xx.xx.2

[Success] xxx.xx.xx.2 --> xxx.xx.xx.3 [Success]

The fix was - When i added the host entry to saying

xxx.xx.xx.2 - api.com

things started working - because all calls to the api were now routed via the proxy server. I dont know why this is happening or how to avoid this. atleast solved my issue.

I checked for setting- i dont have anything in my web.config / machine.config.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download