HttpWebRequest Timeout
随着REST风格的流行,直接通过 HttpWebRequest 进行服务调用的客户端应用越来越多。这里总结一些可能需要费时调查的经验,希望能帮助大家。
1. 用完的HttpWebRequest要Abort()或者要把 Response.Close()
否则会导致请求Timeout。 (HttpWebRequest.Method默认是GET)
static void Main( string [] args) { for ( int i = ; i < ; i++) { Console.Write( "[{0}] Request - " , i + ); TryGet( "https://login.live.com/" ); } Console.Read(); } static void TryGet( object obj) { try { HttpWebRequest webReq = null ; string url = ( string )obj; webReq = (HttpWebRequest)HttpWebRequest.Create(url); webReq.Timeout = * ; var resp = webReq.GetResponse() as HttpWebResponse; resp.Close(); Console.WriteLine( "Get Response StatusCode: {0}({1})" , resp.StatusCode, ( int )resp.StatusCode); } catch (WebException we) { Console.WriteLine( "Get Response StatusCode: {0}({1})" , we.Status, ( int )we.Status); } catch (Exception ex) { Console.WriteLine(ex); } }
上面的代码,会从第3次Request开始出现Timeout,因为GetResponse 后 Stream打开未关闭。
解决方法:上面的代码中加上 resp.Close(); 或者 webReq.Abort(); 就能解决。
2. 多线程中调用 HttpWebRequest 时,需要设置 ServicePointManager.DefaultConnectionLimit 数(默认连接数是 2)。
当多线程请求时,同时的连接数超过Limit时,GetResponse会抛出 Timeout WebException。
// 用多线程同时发出4个请求 WaitCallback methodTarget = new WaitCallback(TryGet); ThreadPool.QueueUserWorkItem(methodTarget, "https://login.live.com/" ); ThreadPool.QueueUserWorkItem(methodTarget, "https://login.live.com/" ); ThreadPool.QueueUserWorkItem(methodTarget, "https://login.live.com/" ); ThreadPool.QueueUserWorkItem(methodTarget, "https://login.live.com/" );
解决方法:在GetResponse()之前设置 ServicePointManager.DefaultConnectionLimit = 100;
3. 当请求一个基于SSL的服务时,默认的验证行为都在 ServicePointManager 定义:
ServicePointManager.CheckCertificateRevocationList = true;
如果请求的服务端证书没有第三方的认证支持,则请求会失败,如果要完全信任服务端证书,则可以将
CheckCertificateRevocationList 设为 false。
4. 可以在 <system.net> 配置节中配置 HttpWebRequest 的属性,包括 WebProxy
<system.net>
<connectionManagement> </connectionManagement> <defaultProxy> <proxy proxyaddress= "http://xxx.xxx.xxx.xxx:xxx" bypassonlocal= "False" /> </defaultProxy> <settings> <httpWebRequest useUnsafeHeaderParsing= "true" /> <servicePointManager checkCertificateName= "true" checkCertificateRevocationList= "true" enableDnsRoundRobin= "true" expect100Continue= "true" useNagleAlgorithm= "true" /> </settings> </system.net>
原文链接:https://www.cnblogs.com/1971ruru/archive/2012/04/11/2442589.html?tdsourcetag=s_pctim_aiomsg#
HttpWebRequest Timeout的更多相关文章
- HttpWebRequest的Timeout和ReadWriteTimeout
HttpWebRequest.Timeout在发起请求开始,如果未从远程请求的URL得到任何数据的情况下,超过Timeout后,触发超时异常 HttpWebRequest.ReadWriteTimeo ...
- HttpWebRequest的timeout和ReadWriteTimeout(转载)
公司[1]一牛人看我的代码,说我设置的timeout有误,还应该设置ReadWriteTimeout.本人很不服,于是上网查看了相关说明. HttpWebRequest httpWebRequest ...
- 通过WebClient/HttpWebRequest实现http的post/get方法
///<summary>/// 通过WebClient类Post数据到远程地址,需要Basic认证: /// 调用端自己处理异常 ///</summary>///<par ...
- C# post请求 HttpWebRequest
//body是要传递的参数,格式"roleId=1&uid=2" //post的cotentType填写: //"application/x-www-form-u ...
- HttpWebRequest和WebClient的区别
HttpWebRequest和WebClient的区别(From Linzheng): 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Creat ...
- HttpWebRequest 请求带OAuth2 授权的webapi
OAuth 2.0注意事项: 1. 获取access_token时,请使用POST private static string GetAuthorization(string username, st ...
- C#中HttpWebRequest的用法详解
原文链接:http://www.cnblogs.com/love201314/p/5029312.html 1.HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数 ...
- HttpWebRequest.ReadWriteTimeout 属性
获取或设置写入或读取流时的超时. 属性值在写入超时或读取超时之前的毫秒数.默认值为 300,000 毫秒(5 分钟). 备注 在写入由 GetRequestStream 方法返回的流时,或在读取由 G ...
- C#中HttpWebRequest的用法详解(转载)
1.HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.2.命名空间:System.Net3.HttpWebRequest对象不是利用new关键字创建 ...
随机推荐
- Python基础Day1—下
六.Python运行 print() 打印命令,输出到屏幕上 操作: 命令提示符-->输入Python-->文件路径 若输入Python回车报错或者提示没有,则Python解释器没有安 ...
- python接口自动化13-data和json参数傻傻分不清
前言 在发post请求的时候,有时候body部分要传data参数,有时候body部分又要传json参数,那么问题来了:到底什么时候该传json,什么时候该传data? 一.识别json参数 1.在前面 ...
- windows nginx
nginx.exe -s stop stop是快速停止nginx,可能并不保存相关信息: nginx.exe -s quit quit是完整有序的停止nginx,并保存相关信息. nginx.exe ...
- Proxmox 命令使用方法
proxmox 虚拟机使用命令介绍 qm <command> <vmid> [OPTIONS] ...
- P1402 酒店之王[网络瘤(正解)/匈牙利(错解)]
题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜. ...
- ted演讲小总结(持续更新_12月15日)
目录 2019年12月1日 星期日 2019年12月2日 星期一 2019年12月3日 星期二 2019年12月8日 星期日 2019年12月15日 星期日(这个演讲相对来说不好理解,因为这类逻辑暂时 ...
- 2019-2020-1 20199301《Linux内核原理与分析》第六周作业
第五章 系统调用的三层机制(下) 1.给MenuOS增加命令 代码如下: rm -rf menu git clone http://github.com/mengning/menu.git make ...
- ORA-12638: Credential retrieval failed 解决办法
ORA-12638 ORA-12638: Credential retrieval failed 身份证明检索失败 解决办法: 修改sqlnet.ora文件(位置:$ORACLE_HOME ...
- 十二.Protobuf3编码
本文档描述了协议缓冲消息的二进制格式.在应用程序中使用Protocol Buffer不需要理解这一点,但是了解不同的Protocol Buffer格式如何影响编码消息的大小会非常有用. 一条简单的信息 ...
- maven的使用和环境搭建
请在博客分类的未分类中找到这篇文章