System.Net.WebException: The operation has timed out  at System.Net.HttpWebRequest.GetResponse()

在请求获取响应结果的时候,超时,具体原因可能就是如下面Jon Skeet所说,

WebResponse implements IDisposable,
so you should use a using statement for it (and for the StreamReader you create from the stream).
If you leave a WebResponse open, it will take up a connection from the connection pool to that host,
and you can end up with timeouts this way.
WebResponse继承IDisposable接口,会释放非托管资源
所以你需要使用声明来创建资源对象
如果你打开WebResponse资源响应,那么他将通过连接池连接主机,使用这个方式,超时将不会成为问题。
应该是这么翻译吧╮(╯_╰)╭
This will close the stream and the response even if an exception is thrown, 
so you'll always clean up the resources (in this case releasing the connection back to the pool) promptly.
这个将会关闭stream和response,即使出现抛出异常的情况
所以你总能够即时清理好资源(释放应用池的连接) 这个方式,尝试了一下,暂时没出现什么问题,算解决了99%吧,等到以后再出现什么问题,再来看。

下面是网上找到的一个解决方案:

http://stackoverflow.com/questions/15493321/system-net-webexception-the-operation-has-timed-out-on-httpwebresponse

Author:Jon Skeet

System.Net.WebException: The operation has timed out on HttpWebResponse

This may well be the problem:

HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();

WebResponse implements IDisposable, so you should use a using statement for it (and for the StreamReader you create from the stream). If you leave a WebResponse open, it will take up a connection from the connection pool to that host, and you can end up with timeouts this way. The fixed code would look like this:

string responseString;
using (var response = myReq.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream())
{
responseString = reader.ReadToEnd();
}
} This will close the stream and the response even if an exception is thrown, so you'll always clean up the resources (in this case releasing the connection back to the pool) promptly.

System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()的更多相关文章

  1. System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE

    I am having problems with a bit of code that accesses a restful web service. Running this code, it e ...

  2. C# post数据时 出现如下错误: System.Net.WebException: 操作超时

    net(客户端)调用IIS(服务端)出现503后,就报操作超时错误 问题描述: 服务端环境: IIS 客户端环境: windowsxp + iis + .net 调用时出现如下错误: System.N ...

  3. Method and system for providing security policy for linux-based security operating system

    A system for providing security policy for a Linux-based security operating system, which includes a ...

  4. 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1

    在WPF中DataGrid 选择事件中获取SelectedItems 报错如下 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转 ...

  5. 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。

    问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...

  6. 配置IIS提示打开目录浏览时的问题:未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的解决办法

    错误消息: 未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的 ...

  7. “System.FormatException”类型的未经处理的异常在 System.IdentityModel.dll 中发生 其他信息: 十六进制字符串格式无效。

    如果你的 WebService 客户端证书配置都没问题,唯独调用接口会出现这个错误 “System.FormatException”类型的未经处理的异常在 System.IdentityModel.d ...

  8. 关于System.getProperty("java.io.tmpdir");的输出,及System.getProperty();参数

    1,首先来介绍下System.getProperty("java.io.tmpdir")输出因为这个输出有点特殊. 理论介绍:他是获取系统临时目录.可以是window的temp,l ...

  9. WPF System.InvalidCastException: 无法将类型为“System.Windows.Media.Color”的对象强制转换为类型“System.Windows.Media.Brush”。

    场景:添加ComboBox样式,界面卡死,日志异常文件如下: -- ::, | ERROR | System.InvalidCastException: 无法将类型为“System.Windows.M ...

随机推荐

  1. jacky自问自答-java并发编程

    1.java Web中线程不是由tomcat这类web容器负责的吗?为什么还要我控制多线程? 答:这个问题很多初学者都会有的疑惑,举一个我以前做的一个需求,java作为中间平台,是socket服务端, ...

  2. 如何设置Windows server 2008 R2登陆密码?

    我第一次用该系统,登陆系统需要设置新密码,科沃怎么设置新密码都是新密码不符合条件,不允许我创建,该怎么办? 答案: 至少6个字符,要包括大小写.符号.数字这四组字符中的三组.如12abAB是有效密码, ...

  3. Winform中导入exicel文件

    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); ...

  4. Hibernate使用createSqlQuery进行模糊查询时找不到数据

    1. 首先明确一点,使用createSqlQuery如下两种方式的占位符都可以使用,这个在官方的文档可以查到. 注意使用模糊查询时,赋值两边不可以添加单引号. Query query = sess.c ...

  5. Install Homebrew

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...

  6. java.lang.OutOfMemoryError: Java heap space 解决方法

    从网上抄过来的,因为经常碰到这个问题,记录一下. java.lang.OutOfMemoryError: Java heap space 解决方法 这个问题的根源是jvm虚拟机的默认Heap大小是64 ...

  7. DataGridView使用技巧四:删除行操作

    一.无条件的删除行 默认时,DataGridView是允许用户进行行的删除操作,选中要删除的行,按Delete键可以删除,该操作没有任何提示(只是删除界面显示的数据,不会真实删除数据库中的数据).如果 ...

  8. 每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠

    每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠 (除非有意使多个分支重叠). #include <iostream> /* run this program us ...

  9. 【转载】C#进阶系列——动态Lamada(二:优化)

    前言:前几天写了一篇动态Lamada的文章C#进阶系列——动态Lamada,受园友xiao99的启发,今天打算来重新优化下这个动态Lamada的工具类.在此做个笔记,以免以后忘了. 一.原理分析 上篇 ...

  10. SSL双向认证Java实现 Tomcat篇

    双向验证,在客户机连接服务器时,客户机验证服务器的证书,服务器验证客户机的证书,链接双方都要对彼此的数字证书进行验证,保证这是经过授权的才能够连接. 1. 生成服务器端的keystore和trusts ...