C#中HttpWebRequest的GetRequestStream执行的效率太低,甚至偶尔死掉
【问题】
C#中,提交对应的POST类型http请求之前,会执行:
Stream postDataStream = req.GetRequestStream();
然后填充对应的post数据,再提交http的请求。
但是调试的时候,发现每次执行GetRequestStream都很慢。
慢也就算了,结果最近发现,某次,执行req.GetRequestStream();死掉。
【解决过程】
1.网上找了找,根据 GetRequestStream的效率为什么这么低? 的解释,说是.NET每次会自动搜索代理,所以很慢,此处没有设置代理的话,应该直接复制为空:
req.Proxy = null;
然后再去执行GetRequestStream,这样就不会慢了。
然后去试了试,好像是这个效果。
2.结果第二天同样的代码,去调试,结果GetRequestStream的地方,还是死掉了。
原因暂未知。
3.后来的情况是,同样的代码,再次运行,就不死了,虽然也还是有时候效率低,反应慢,但是不会死了。具体根本原因,感觉还是没有找到。
4. 后来的多次调试,发现对于:
req.Proxy = null;
好像也没啥效果。
只是碰巧找到了,关于设置proxy为null的原始的官方的解释:
”C# 3.0 in a Nutshell, Third Edition: A Desktop Quick Reference“的Chapter 14. Networking:
Warning If you don’t have a proxy, you must set the Proxy property to null on all WebClient and WebRequest objects. Otherwise, the Framework may attempt to "auto-detect" your proxy settings, adding up to 30 seconds to your request. If you’re wondering why your web requests execute slowly, this is probably it! |
但是此处,对于我所遇到的,执行GetRequestStream会死掉的问题,还是没有啥帮助。
5.多次调试得到的结果是,如果是对于:
Stream postDataStream = req.GetRequestStream();
是单步调试的话,好像很少会死掉.
而如果直接运行,没有打断点,那么经常容易死掉.
6.参考:GetRequestStream throws Timeout exception randomly,去试了:
1
2
3
4
5
|
using (Stream postDataStream = req.GetRequestStream()) { postDataStream.Write(postBytes, 0, postBytes.Length); postDataStream.Close(); } |
还是会死掉。
7.网上看到:HttpWebRequest.GetRequestStream() hangs,看起来此问题,好像已经是官方承认的一个bug。
不过即使真的是bug,其解释说是在.NET 4.0 beta2才解决掉,而我此处无法用.NET 4.0,只能用.NET 2.0/3.0/3.5,所以,如果真是bug的话,那就真的悲剧了。。。
还是先去看看,有没有其他解决办法再说吧。
8.试了如下代码:
1
2
3
4
5
6
7
8
9
10
11
|
try { Stream postDataStream = req.GetRequestStream(); postDataStream.Write(postBytes, 0, postBytes.Length); postDataStream.Close(); } catch (WebException ex) { MessageBox.Show(ex.Message); } |
单步调试了:
Stream postDataStream = req.GetRequestStream();
发现经过很长的时间之后,捕获到了异常WebException ex,出现了“Unable to connect to the remote server”错误,其详细信息如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
ex {"Unable to connect to the remote server"} System.Net.WebException base {"Unable to connect to the remote server"} System.InvalidOperationException {System.Net.WebException} base {"Unable to connect to the remote server"} System.SystemException {System.Net.WebException} base {"Unable to connect to the remote server"} System.Exception {System.Net.WebException} _className null string _data null System.Collections.IDictionary _dynamicMethods null object _exceptionMethod null System.Reflection.MethodBase _exceptionMethodString null string _helpURL null string _HResult -2146233079 int _innerException {"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 65.55.254.42:443"} System.Exception {System.Net.Sockets.SocketException} _message "Unable to connect to the remote server" string _remoteStackIndex 0 int _remoteStackTraceString null string _source null string _stackTrace {sbyte[96]} object {sbyte[]} _stackTraceString null string _xcode -532459699 int _xptrs 0 System.IntPtr Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} HelpLink null string HResult -2146233079 int InnerException {"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 65.55.254.42:443"} System.Exception {System.Net.Sockets.SocketException} IsTransient false bool Message "Unable to connect to the remote server" string Source "System" string StackTrace " at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)\r\n at System.Net.HttpWebRequest.GetRequestStream()\r\n at InsertSkydriveFiles.skydrive.beforeUploadFile()" string TargetSite {System.IO.Stream GetRequestStream(System.Net.TransportContext ByRef)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo} Static members InternalStatus ServicePointFatal System.Net.WebExceptionInternalStatus m_InternalStatus ServicePointFatal System.Net.WebExceptionInternalStatus m_Response null System.Net.WebResponse m_Status ConnectFailure System.Net.WebExceptionStatus Response null System.Net.WebResponse Status ConnectFailure System.Net.WebExceptionStatus |
此处觉得很不能理解的事情是,我只是调用GetRequestStream,获得request的stream,又不是来自服务器端的response,为何也要去链接remote server,而不是本地调用一个库函数,获得对应的stream???
【后记 2012-03-01】
后来发现,好像此问题,和GetResponse偶尔死掉,是同一个原因,然后也解决了这个问题,具体过程和办法,请参考:
http://www.cnblogs.com/summer_adai/archive/2013/04/26/3045253.html
原文连接:https://www.cnblogs.com/summer_adai/archive/2013/04/26/3045261.html
原文地址:https://www.cnblogs.com/1175429393wljblog/p/11678266.html
- 使用jQuery Uploadify在ASP.NET 上传附件
- 利用SSH Filesystem实现远程文件系统
- 来看看机智的前端童鞋怎么防盗
- qq平台登录
- 重构实践:体验interface的威力(二)
- Visual Studio 2013 Web开发
- 初识Opserver,StackExchange的监控解决方案
- OstrichNet 简易统计信息收集工具
- 百度地图开发1
- 开源消息队列:NetMQ
- 自然语言处理如何检查拼写错误?(Tensorflow实例教程、源代码)
- 仿qq登录界面
- 搭建Linux+Jexus+MariaDB+ASP.NET[LJMA]环境
- WindowsMobile/Win Form-界面自适应
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法