HttpClient在发送请求时抛出错误。" [英] HttpClient throwing "An error occurred while sending the request."

查看:73
本文介绍了HttpClient在发送请求时抛出错误。"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三层应用程序体系结构。

我的客户端-->我的服务A(睡觉托管在iIS中)-->其他团队的服务X(睡觉)。

服务A是ASP.NET 4.6.1框架,而不是ASP.NET核心。

客户端正在使用HttpClient与A通信,A正在使用HttpClient与X通信。

客户端向A和X发出几乎2500个对我的服务的调用。

在2500个随机调用中,服务A(可能是10个调用)失败,出现以下异常。它是不可复制的。

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> 
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a 
receive. ---> System.IO.IOException: Unable to read data from the transport connection: An     
established connection was aborted by the software in your host machine. ---> 
System.Net.Sockets.SocketException: An established connection was aborted by the software in your 
host machine
at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags 
socketFlags, AsyncCallback callback, Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback 
callback, Object state)
 --- End of inner exception stack trace ---
at System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult)
at System.Net.TlsStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace --

这是我的服务A电话。iis中的a调用下面的代码挡路,并由每个请求调用它。X将根据用户获取用户凭据并返回数据,因此我们不会在调用之间共享HttpClient。

var user = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
            System.Security.Principal.WindowsIdentity.RunImpersonated(user.AccessToken, () =>
      {
        static HttpClient Client = new HttpClient();
        static string CallX(string[] args)
        {
            HttpClientHandler handler = new HttpClientHandler
            {
                UseDefaultCredentials = true
            };    

            Client = new HttpClient(handler)
            {
                BaseAddress = new Uri("http://XserviceUrl/api/")
            };
            Client.Timeout = TimeSpan.FromSeconds(600);    
            var result = Client.PostAsync("Fake X controller"
                , new StringContent(JsonConvert.SerializeObject(args)
                , Encoding.UTF8, "application/json")).Result;    

            result.EnsureSuccessStatusCode();

            var json = result.Content.ReadAsStringAsync().Result;
            return DosomethingWithResult(json);    
        }
    });

我尝试的内容:

因此POST建议的某些内容可能是超时问题。因此,我在客户端和服务A中添加了600秒。我还将IIS请求超时从默认的2分钟更改为10(600秒)。

推荐答案

经过一番调查,我解决了问题。 当A向X发送请求时,A正在设置Connection:Keep-Alive,而X在标头中使用Connection:Close属性进行响应。

因此,在一些调用之后,A耗尽了打开的TCP连接,并随机抛出错误。

(小提琴手帮我想出来的)

所以我所要做的就是设置HttpClient的ConnectionClose属性

_client.DefaultRequestHeaders.ConnectionClose = true;

这篇关于HttpClient在发送请求时抛出错误。"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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