I am trying to make a GET
request to a RESTful service but the .NET HttpClient
is receiving a timeout, but via Postman this returns an expected response (an error response, since I'm not yet "logged in") within seconds.
HttpClient
I still receive a timeout.HttpClient
I still get a timeout from the server ((504) Gateway Timeout
).Client = new HttpClient(new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip
});
var objTask = Client.GetStringAsync(objUrl);
objTask.Wait();
strResponse = objTask.Result;
strResponse = Client.GetStringAsync(objUrl).Result;
Why am I receiving a timeout via HttpClient?
Our WebApi is calling our WCF Service, which in turn is calling the 3rd party RESTful service via HttpClient---for security reasons I can't call the 3rd party API directly from our WebApi controller. The website is based on the .NET 4.0 Framework.
With lots of help from @jdweng and @gunr2171, I've realised the issue was due to the proxy stored in the App.Config
on the self-hosted WCF Services. If I commented this out, I wouldn't get a time-out issue.
The HttpClient
constructor can take in a HttpClientHandler
, which has a Proxy
property where you can bypass the local proxy as so:
return new HttpClient(new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip,
Proxy = new WebProxy()
{
BypassProxyOnLocal = true
}
})
{
BaseAddress = new Uri("https:\\my.service")
};