Reputation: 1681
The problem:
In a WebApi controller i am trying to make a http request to another website using HttpClient - But it allways fails, and says that the host doesnt respond, or that it failed to setup a connection.
How to reproduce:
In Visual Studio (Enterprise on this end) 2017, Create a new Project using the Azure Web Api Template (File -> new -> Project -> ASP.NET Web Application (.NET Framework) -> Ok -> Azure Web Api.
Make sure .NET Framework 4.7.1 is selected. (I have not tested with any other frameworks except .net core)
In the ValuesController, from the Get method, make a call to any URL and try to respond with the Result StatusCode.
Code::
public class ValuesController : ApiController
{
// GET api/values
[SwaggerOperation("GetAll")]
public IEnumerable<string> Get()
{
using (var client = new HttpClient())
{
var result = client.GetAsync("https://www.google.com").Result;
return new string[] { result.StatusCode.ToString(), "value2" };
}
}
}
Debugging:
I did a fair bit of searching around this error, but with no result on my end.
Any tips?
Exception Details:
System.AggregateException occurred
HResult=0x80131500
Message=One or more errors occurred.
Source=<Cannot evaluate the exception source>
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at WebApplication2.Controllers.ValuesController.Get() in C:\Users\<user>\documents\visual studio 2017\Projects\WebApplication2\WebApplication2\Controllers\ValuesController.cs:line 19
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
Inner Exception 1:
HttpRequestException: An error occurred while sending the request.
Inner Exception 2:
WebException: Unable to connect to the remote server
Inner Exception 3:
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 216.58.209.99:443
Upvotes: 1
Views: 3602
Reputation: 1681
Problem Solved - Solution:
Im behind a proxy, and it seems like either IIS Express or something within .net Framework, does not pick up the proxy settings that is defined in the system automatically like .net Core did, and the console application.
Adding the proxyaddress to web.config
with the proxy address did the trick.
<system.net>
<defaultProxy>
<proxy usesystemdefault="True" proxyaddress="http://www-proxy.somedomain.com:80" bypassonlocal="False"/>
<bypasslist>
<add address="[a-z]+\.somedomain\.com$"/>
<add address="[a-z]+\.somedomain\.net$"/>
</bypasslist>
</defaultProxy>
</system.net>
Hope this helps anyone else running in to this issue!
Upvotes: 4