HttpWebRequest多线程性能问题,请求超时的错误

转自:http://hi.baidu.com/ju_feng/blog/item/b1c41dbf09ad9e0119d81fb0.html

通过Netstat –abn | find “:443” 如果有两个Time_Wait 和CLOSE_WAIT状态的网络连接,则该服务就不可用

解决方法:

1 HttpWebRequest的缺省连接只有两个,因此在多线程并发的情况下只有两个并发请求

可以通过 System.Net.ServicePointManager.DefaultConnectionLimit = 20

设置网络HttpWebRequest的网络连接池的个数

2 HttpWebRequest 的请求因为网络问题导致连接没有被释放则会占用连接池中的连接个数,导致并非连接数量减少


可以通过如下方法来修改此问题 :

2.1 给ServicePointManager设置KeepLive检测,在网络没有响应的情况下关闭该连接

System.Net.ServicePointManager.SetTcpKeepAlive(true, _keepLiveTime, _intervalTime);

3 定时检测HttpWebRequest的ServicePoint的健康状况

//ServicePoint point = webReq.ServicePoint;

//string connectionsGroupName = webReq.ConnectionGroupName;

//if (point != null)

//{

// if (point. > 15)

// point.CloseConnectionGroup(connectionsGroupName);

//}

4 使用HttpWebRequest 一定要保证GetRequestStream和GetResponse对象关闭,否则容易造成连接处于CLOSE_WAIT状态


using (Stream stream = webReq.GetRequestStream())

{

stream.Write(buffer, 0, buffer.Length);

}

HttpWebResponse response = null;

try

{

response = webReq.GetResponse() as HttpWebResponse;

}

finally

{

try

{

if (response != null)

response.Close();

//ServicePoint point = webReq.ServicePoint;

//string connectionsGroupName = webReq.ConnectionGroupName;

//if (point != null)

//{

// if (point. > 15)

// point.CloseConnectionGroup(connectionsGroupName);

//}

}

catch

{

}

}


I am wondering whether there is a way to handle broken (or better: closed) TCP connections that reside in the connection pool (ServicePoint) used by a HttpWebRequest instance. What I have seen on Windows 2003 Server systems is that under certain circumstances, all sockets used to connect to a web service are in CLOSE_WAIT state and all subsequent HttpWebRequests fail with a timeout, no new connections to the web service were opened. Looking at the problem with WireShark, I couldn't see any HTTP traffic even though the application kept on making requests that all ended in a timeout.

I have tried to reproduce this behaviour on a development system by writing a simple HTTP server that closes the socket after it has sent a proper HTTP 1.1 response. If you do this, you will see the same behavior that I have described above. Of course, HTTP 1.1 assumes that by default connections are kept alive unless the server explicitly sends a "Connection: Close" header. Thus, the client-side would assume that the connection should be kept alive although the server (that does not function according to HTTP 1.1) already closed it. What stuns me is that the HttpWebRequest, or more specifically the ServicePoint does not recover from such a situation (on Windows Vista/2003 Server the client-side sockets stay in CLOSE_WAIT until they're killed by the OS, on another development machine running the Windows 7 RC they're killed by the OS rather quickly and are just gone, no new ones will be created in either case).

I have tried various things on the client-side, so far I could not solve this problem (except by disabling keep alive in each HttpWebRequest by setting the "KeepAlive" property to "false"). The following list compiles what I've tried to recover from the situation described above:

- Various combinations of using() blocks to dispose all resources (WebResponse object, stream retrieved by GetResponseStream(), StreamReader that was used to read from the stream), as well as using the WebResponse.Close() method call that do everything required from my understanding of the MSDN documentation.
- Calling CloseConnectionGroup() on the ServicePoint used by the HttpWebRequest to somehow "kill" the connections
- Temporarily disabling keep alives to recover
- Increasing the number of connections in the pool of the ServicePoint so that it doesn't stop the system from functioning properly if some connections die
- Enabling TCP keep alives on the ServicePoint
- Using a lower ConnectionLeaseTimeout on the ServicePoint (works correctly in general, fails to do anything when connections are closed)

Thus, I'd like to ask here whether someone faced similar problems and might know a way to overcome such issues. I'd also like to know whether I am hunting ghosts here, although I still doubt it at the moment. I also don't see how a socket could get into a CLOSE_WAIT state without the remote communication partner closing the connection (i.e. sending FIN) and I don't understand why Vista/2003 Server do not seem to respond to this by sending a FIN and receiving an ACK to transition from CLOSE_WAIT to LAST_ACK and then to CLOSED. The HTTP server I wrote does not tinker with the LingerState of the TcpClient, default behavior should apply when it comes down to gracefully closing sockets.

Maybe I am missing some very important point about this whole thing, I'd love to be enlightened :-). If you need any further info, I'm happy to supply it.

PS: I have done all of my tests in Visual Studio 2008 linking both .NET 2.0 and .NET 3.5, it doesn't make a difference from what I can tell.

Cheers!

HTTP连接状态:

// //服务器开通KeepLive. 第一个参数是客户端开启Keeplive,
// //第二个参数是客户端是否关闭网络连接
////Server: Close_Wait Client:Fin_Wait_2
//HttpRequestTest.TestHttpConnect(false, false);
////Server: Close Client: Time_Wait
//HttpRequestTest.TestHttpConnect(false, true);
////Server: ESTABLISHED Client: ESTABLISHED
//HttpRequestTest.TestHttpConnect(true, true);
//Server: Close Client: Time_Wait
// HttpRequestTest.TestHttpConnect(false, true);


//服务器关闭KeepLive. 第一个参数是客户端开启Keeplive,
//第二个参数是客户端是否关闭网络连接
////Server: Close_Wait Client:Fin_Wait_2
//HttpRequestTest.TestHttpConnect(false, false);
////Server: Close Client: Time_Wait
//HttpRequestTest.TestHttpConnect(false, true);
////Server: Close Client: Time_Wait
//HttpRequestTest.TestHttpConnect(true, true);
////Server: Close Client: Time_Wait
//HttpRequestTest.TestHttpConnect(false, true);

REF:

http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/5c77f187-add4-46cb-a3f8-93e78910eddc

http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx

http://www.cnblogs.com/zealic/archive/2008/05/01/1107942.html

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/310974/viewspace-1043123/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/310974/viewspace-1043123/

推理引擎终极对决:TensorRT vs ONNX Runtime,YOLOv8速度差30%的核心原因与实操指南 若你使用NVIDIA GPU,且追求极致速度和低延迟,TensorRT是唯一选择,尤其是边缘设备和实时场景;若你需要跨硬件部署、快速验证原型,或预算有限(无需NVIDIA GPU),ONNX Runtime是性价比之王;对于YOLOv8这类检测模型,TensorRT的FP16/INT8量化是“免费的速度提升”,建议优先采用。最后提醒:推理引擎的优化是“细节决定成败”——同样的引擎,不同的预处理方式、显存配置、批大小,都会导致10%-20%的性能差异。 阅读详情

相关推荐

STM32F103单片机驱动TM1637数码管显示模块

最近项目中需要用到数码管显示,于是买了一个TM1637芯片驱动的四位数码显示模块,现将调试过程记录一下,方便以后参考。 使用的单片机是STM32F103C8T6最小系统 使用的数码管模块是TM1637四位数码管显示模块 实际运行效果 下面先看一下TM1637和数码管连接的具体线路图 实际使用的模块没有带按键,只用了4个数码管,模块和单片机连接只需要4根线VCC、GND、CLK、DIO。芯片和单片机通信使用的是I2C总线,下面就来说一下如何...

HXYDJ的博客 1万+

C#中HttpWebRequest性能问题

用到HttpWebRequest的地方,都应该紧跟一句 httpWebRequest.ServicePoint.ConnectionLimit = int.Max; .net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows  7 下默认是2,在服务器操作系统上默认为10. 如果不

深南大道 3931

从寄存器到驱动:深入解读RFSoC RF-ADC阈值与校准API的底层逻辑

本文深入解析Xilinx RFSoC中RF-ADC的阈值检测与校准API的底层逻辑,涵盖寄存器映射、驱动实现及Gen3增强功能。通过详细代码示例和寄存器操作流程,揭示如何精准控制信号阈值检测与校准时序,提升系统稳定性与性能。特别探讨RF数据转换器在无线通信和雷达信号处理中的关键应用。

weixin_30552811的博客 687

C#中HttpWebRequest最大并发和性能

C#中HttpWebRequest最大并发和性能

softshow1026的博客 944

西门子博图TIA Portal V18实战:用LAD梯形图快速上手一个电机启停控制程序

本文详细介绍了如何在西门子博图TIA Portal V18中使用LAD梯形图快速实现电机启停控制程序。从项目创建、硬件组态到LAD编程和仿真调试,逐步引导读者掌握PLC编程的核心逻辑,特别适合工业自动化领域的初学者快速上手。

weixin_30617737的博客 690

期权专题9:雪球期权(一)(BSM+蒙特卡罗定价)

python;雪球定价;蒙特卡罗

mhmh123789的博客 8102

解开多线程时webRequest最多只能同时进行两个事务的限制

在Form_Load中加入 System.Net.ServicePointManager.DefaultConnectionLimit = 1000; 转载于:https://www.cnblogs.com/waixingehao/archive/2011/10/08/2202235.html

weixin_30784945的博客 302

rk3566-11.0新增device分区

1.增加块设备 2.修改根目录节点权限 3.开机启动自动挂载 增加块设备 1.修改parameter.txt文件,新增块设备大小及命名 device/rockchip/rk356x/rk3566_r/parameter.txt FIRMWARE_VER: 11.0 MACHINE_MODEL: rk3566_r MACHINE_ID: 007 MANUFACTURER: rockchip MAGIC: 0x5041524B ATAG: 0x00200800 MACHINE

weixin_51115260的博客 3384

HttpClient在多线程环境下踩坑总结

HttpClient在多线程环境下踩坑总结问题现场在多线程环境下使用HttpClient组件对某个HTTP服务发起请求,运行一段时间之后发现客户端主机CPU利用率呈现出下降趋势,而不是一个稳定的状态。而且,从程序日志中判断有线程处于夯住的状态,应该是被阻塞了。CPU使用率逐步下降 问题排查一开始找不到原因,怀疑是多线程并发导致的死锁问题,但是通过代码审查...

weixin_34071713的博客 2572

如何将TDengine集成到Thingsboard?

1 概述 ThingsBoard是用于数据收集、处理、可视化和设备管理的开源物联网平台。它通过行业标准的物联网协议MQTT、CoAP和HTTP实现设备连接,并支持云和本地部署。 ThingsBoard迄今没有导入专业的时序数据库应用,用户可选择的只有PostgreSQL和Cassandra。虽然针对PostgreSQL引入了TimeScale插件,但由于其不是按照宽表的方式组织数据记录,TimeScale的优势根本发挥不出来。总之,这两种数据库不具备高效的数据吞吐能力。 而且,Thingsboard在

Wearable_device的博客 4767

一分钟了解 “提示词工程”、“上下文工程”、“驾驭工程”

在大模型刚刚进入大众视野的初期,开发者面临的最大挑战是如何让模型听懂人类的指令。这一阶段被称为提示词工程时代。其核心思想非常直观:通过精心设计输入的文本提示,引导模型输出符合预期的结果。这一时期的开发者更像是“提示词巫师”,他们热衷于寻找所谓的“魔法咒语”。典型的技巧包括角色设定、思维链、少样本学习以及输出格式约束。例如,仅仅在问题前加上一句“请一步步思考”,模型的推理能力往往就能得到显著提升。然而,随着应用场景的复杂化,提示词工程的局限性逐渐暴露。

h a 6 6 6 c k 为 你解决复杂难题! 568

C# HttpWebRequest请求超时解决办法

request.GetResponse();超时问题的解决,和HttpWebRequest多线程性能问题,请求超时的错误, 解决办法 1.将http的request的keepAlive设置为false //如果不是必须的要keepalive的,那么就要设置KeepAlive为false: 2.修改System.Net.ServicePointManager.DefaultConn...

weixin_30824277的博客 6446

YOLOv11改进 | Conv篇 | AKConv轻量级架构下的高效检测(附代码 + 修改方法 + 二次创新)

本文给大家带来的改进内容是AKConv是一种创新的变核卷积,它旨在解决标准卷积操作中的固有缺陷(采样形状是固定的),AKConv的核心思想在于它为卷积核提供了任意数量的参数和任意采样形状,能够使用任意数量的参数(如1, 2, 3, 4, 5, 6, 7等)来提取特征,这在标准卷积和可变形卷积中并未实现​​。AKConv能够根据硬件环境,使卷积参数的数量呈线性增减非常适用于轻量化模型的读者)。

Snu77的博客 1万+

System.Net.HttpWebRequest.GetRequestStream超时问题

错误 引发的异常:“System.Net.WebException”(位于 System.dll 中) 操作超时 System.Net.WebException: 操作超时 在 System.Net.HttpWebRequest.GetResponse() 在 tools.WebUtils.post1(String url, Byte[] fileContentByte) 位置...

向阳而生 4171

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

已解决】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法

主页 > 工作和技术 > 网站技术 > 【已解决】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法 【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response:

QJ.Ran的博客 2168

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

【已解决】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法...

【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (HttpWebResponse)req.GetResponse(); 之前的多次调试,一直都是可以正常获得对应的response,然后读取html页面的。 但是后来几次的调试,在没有改变代码的前提下,...

weixin_30558305的博客 494

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

已解决】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法...

主页 > 工作和技术 > 网站技术 > 【已解决】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法 【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对...

weixin_30439131的博客 2822

HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法 【问题】

【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (HttpWebResponse)req.GetResponse(); 之前的多次调试,一直都是可以正常获得对应的response,然后读取html页面的。 但是后来几次的调试,在没有改变代码的前提下,...

LongtengGensSupreme博客 1019

【转载】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法

【转载】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法 https://www.cnblogs.com/summer_adai/archive/2013/04/26/3045253.html 【转载】HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法 【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的h.

kuangben2000的博客 566

HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的

转自:http://blog.sina.com.cn/s/blog_6fe1bc2e0101emc5.html   【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (HttpWebResponse)req.GetResponse(); 之前的多次调

Kinley.H的专栏 1217

HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法

【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (HttpWebResponse)req.GetResponse(); 之前的多次调试,一直都是可以正常获得对应的response,然后读取html页面的。 但是后来几次的调试,在没有改

上善若水 的专栏 1万+

C# 发送HTTP请求超时解决办法

request.GetResponse();超时问题的解决,和HttpWebRequest多线程性能问题,请求超时的错误, 解决办法 1.将http的request的keepAlive设置为false //如果不是必须的要keepalive的,那么就要设置KeepAlive为false: 2.修改System.Net.ServicePointManager.DefaultConnectio...

weixin_30700977的博客 2725

HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法...

HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法 2015-06-17 15:382002人阅读评论(0)收藏举报 分类: POST(2)编程语言(143)Http(7) 【问题】 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http...

dianlum5843的博客 332
上一篇: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NE
下一篇: 当使用curl_multi_exec操作时,需要获取返回的错误信息时,通过curl_errno会发现结果始终为0...
cuichen4524
博客等级 码龄11年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值