0
votes

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.

2
So you are calling the same API endpoint from the same machine, using AJAX it works, using C# it doesn't. Correct?MrZander
Also, do you have access to the server config or is this an external API?MrZander
@MrZander - Correct. Same Api - Same call.. Same url . It is an External api that is owned by a different team. I can ask them to check for something in particular. but i donot have direct accessNight Monger
A firewall configuration?T.S.
firewall port could be configured to let certain applications in or keep them outT.S.

2 Answers

1
votes

If you are doing the same API call from the same machine and one method is working while the other isn't, chances are you are not sending something that the server wants.

When you use an AJAX call, your browsers appends a lot of auxiliary information that you need to replicate if you want the API to respond. For example, cookies and the User agent string. The server is detecting that you are not a browser, and rejecting your request. There are plenty of valid reasons for this, like trying to stop automated bots from hitting their API.

You can see what your browser is sending by using the developer tools. For example, in Chrome, you can view the network request headers like this:

Chrome

Or, it could still be your firewall restricting your application and allowing your browser through. Try adding a rule for your application explicitly.

0
votes

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.