Wep API无法从HttpClient调用返回数据 [英] Wep API fails to return data from HttpClient call

查看:68
本文介绍了Wep API无法从HttpClient调用返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么会发生以下问题:

I have not been able to figure out why the following issue is happening:

我已经创建了一个简单的Web api控制器(.Net Framework 4.6.1)

I have created a simple web api controller (.Net Framework 4.6.1)

public class TestController : ApiController
{
    // GET api/<controller>
    public async Task<string> Get()
    {
        var url = "http://api.plos.org/search?q=title:DNA";
        using (var httpClient = new HttpClient())
        {
            using (var response = await httpClient.GetAsync(url))
            {
                using (var content = response.Content)
                {
                    var result = await content.ReadAsStringAsync();
                    return result;

                }
            }
        }
    }
}

运行上面的代码时,大约20秒后出现以下错误.

When I run the code above I get the following error after about 20 seconds.

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "An error occurred while sending the request.",
    "ExceptionType": "System.Net.Http.HttpRequestException",
    "StackTrace": "   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at restapitest2.Controllers.TestController.<Get>d__0.MoveNext() in C:\\restapitest2\\restapitest2\\Controllers\\TestController.cs:line 19\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Unable to connect to the remote server",
        "ExceptionType": "System.Net.WebException",
        "StackTrace": "   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)",
        "InnerException": {
            "Message": "An error has occurred.",
            "ExceptionMessage": "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.74.38.80:80",
            "ExceptionType": "System.Net.Sockets.SocketException",
            "StackTrace": "   at System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult)\r\n   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)\r\n   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)"
        }
    }
}

当我创建一个dotnet核心Web API控制器时,相同的代码可以正常工作.

When I create a dotnet core web api controller the same code works fine.

这是点网核心代码的样子.

This is what the dot net core code looks like.

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public async Task<ActionResult<string>> Get()
    {
        var url = "http://api.plos.org/search?q=title:DNA";
        using (var httpClient = new HttpClient())
        {
            using (var response = await httpClient.GetAsync(url))
            {
                using (var content = response.Content)
                {
                    var result = await content.ReadAsStringAsync();
                    return result;

                }
            }
        }
    }
}

奇怪的是,如果我使用内部URL,则两个api解决方案都会返回数据.

What is strange is that if I use an internal URL both api solutions return data.

任何帮助将不胜感激

推荐答案

您确定自己没有代理吗?

Are you sure you aren't behind a proxy?

您确定您的DNS服务器可以查找 api.plos.org 吗?

Are you sure that your DNS server can look up api.plos.org?

更深入地检查您的异常,检查其内部异常将帮助您了解DNS问题还是代理问题.

Check your exception more deeply, checking its inner exceptions will help you understand is that a DNS problem or a proxy problem.

如果您位于代理后面,请不要忘记为HttpClient设置代理.

If you are behind a proxy don't forget to set proxy for HttpClient.

更新1:

连接尝试失败,因为被连接方未一段时间后正确响应或建立连接失败,因为连接的主机未能响应216.74.38.80:80

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.74.38.80:80

每次在计算机上获得代理时,我都会遇到此错误.由于您说过如果您运行提琴手,一切都会正常",我假设您需要从Windows Lan Settings 中删除代理设置.

I faced this error every time that I got a proxy on my machine. Since you said "if you run fiddler everything works fine" I assume you need to remove proxy settings from windows Lan Settings.

  1. run 中,键入 inetcpl.cpl ,然后按Enter.
  2. 转到"连接"标签.
  3. 对话框底部,有一个 Lan Settings 按钮.点击它.
  4. 在"LAN设置"对话框中,确保已取消选中所有复选框,尤其是对LAN使用代理.
  1. In the run type inetcpl.cpl and press enter.
  2. Go to the Connections tab.
  3. At the bottom of the dialog, there is Lan Settings button. Click on it.
  4. In the LAN settings dialog make sure you have unchecked all the checkboxes especially use proxy for your lan.

离题推荐:

HttpClient旨在实例化一次并在整个过程中重复使用应用程序的生命周期.为实例化HttpClient类每个请求都会耗尽大量的可用插槽负载.这将导致SocketException错误.下面是一个例子正确使用HttpClient.

HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly.

public class GoodController : ApiController
{
    // OK
    private static readonly HttpClient HttpClient;

    static GoodController()
    {
        HttpClient = new HttpClient();
    }
}

这篇关于Wep API无法从HttpClient调用返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆