从Web Api项目使用C#HttpClient时不会解析主机
问题: 在一个WebApi控制器中,我试图使用HttpClient向另一个网站发出http请求,但它总是失败,并且说主机没有响应,或者它没有建立连接 如何复制:从Web Api项目使用C#HttpClient时不会解析主机,c#,.net,asp.net-web-api,dotnet-httpclient,C#,.net,Asp.net Web Api,Dotnet Httpclient,问题: 在一个WebApi控制器中,我试图使用HttpClient向另一个网站发出http请求,但它总是失败,并且说主机没有响应,或者它没有建立连接 如何复制: System.AggregateException occurred HResult=0x80131500 Message=One or more errors occurred. Source=<Cannot evaluate the exception source> StackTrace: at
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
在Visual Studio(企业版)2017中,使用Azure Web Api模板(文件->新建->项目->ASP.NET Web应用程序(.NET Framework)->确定->Azure Web Api)创建一个新项目
确保选择了.NETFramework4.7.1。(除了.NETCore之外,我没有使用任何其他框架进行测试)
在ValuesController中,从Get方法调用任何URL,并尝试使用结果状态码进行响应
代码::
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" };
}
}
}
公共类值控制器:ApiController
{
//获取api/值
[招摇过市操作(“GetAll”)]
公共IEnumerable Get()
{
使用(var client=new HttpClient())
{
var result=client.GetAsync(“https://www.google.com三、结果;
返回新字符串[]{result.StatusCode.ToString(),“value2”};
}
}
}
调试:
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
- 我创建了一个单独的控制台应用程序,并在那里做了同样的事情
- 我创建了一个单独的.net核心web api项目,并在那里做了同样的事情。[Works]
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
发生System.AggregateException异常
HResult=0x80131500
消息=发生了一个或多个错误。
来源=
堆栈跟踪:
位于System.Threading.Tasks.Task.ThrowifeException(布尔值IncludeTaskCanceledException)
位于System.Threading.Tasks.Task`1.GetResultCore(布尔waitCompletionNotification)
在System.Threading.Tasks.Task`1.get_Result()中
在C:\Users\\documents\visual studio 2017\Projects\WebApplication2\WebApplication2\Controllers\ValuesController.cs中的WebApplication2.Controllers.ValuesController.Get()处:第19行
在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c\u DisplayClass10.b\u 9(对象实例,对象[]方法参数)
位于System.Web.Http.Controller.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,对象[]参数)
位于System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken CancellationToken)
内部异常1:
HttpRequestException:发送请求时出错。
内部例外2:
WebException:无法连接到远程服务器
内部例外3:
SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应216.58.209.99:443
问题已解决-解决方案: 代理背后的Im,似乎是IIS Express或.net Framework中的某个东西,不会像.net Core和控制台应用程序那样自动获取系统中定义的代理设置 使用代理地址将代理地址添加到
web.config
,成功了
<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>
希望这能帮助其他遇到此问题的人!问题已解决-解决方案: 代理背后的Im,似乎是IIS Express或.net Framework中的某个东西,不会像.net Core和控制台应用程序那样自动获取系统中定义的代理设置 使用代理地址将代理地址添加到
web.config
,成功了
<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>
希望这能帮助其他遇到此问题的人!什么是确切的、完全的异常?为什么要用
.Result
阻止异步调用?这只是我测试不同方法的结果,编辑将其放入此处。使其异步不会改变结果。附加到原始帖子的异常:)当我尝试转到该IP地址时,该站点出现问题。它在后台尝试一些失败的xhr调用。我无法解释为什么它适用于其他项目类型。如果您找到了一个真正的、仅限于json的服务,比如SpaceX,该怎么办?如果你想知道例外情况中出现的IP,该网站是www.google.no!异常仅显示解释的地址。您可能需要读取-您使用的结果
是潜在死锁的典型示例。什么是确切和完整的异常?为什么你要用.Result
阻止异步调用?这只是我测试不同方法的结果,编辑将其放入这里。使其异步不会改变结果。附加到原始帖子的异常:)当我尝试转到该IP地址时,该站点存在问题。它在后台尝试一些失败的xhr调用。我无法解释为什么它适用于其他项目类型。如果您找到了一个真正的、仅限于json的服务,比如SpaceX,该怎么办?如果你想知道例外情况中出现的IP,该网站是www.google.no!异常仅显示解释的地址。您可能需要读取-您使用的Result
是潜在死锁的典型示例。