HttpWebRequest:请求被中止:请求被取消 [英] HttpWebRequest: The request was aborted: The request was canceled

查看:266
本文介绍了HttpWebRequest:请求被中止:请求被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一种中间人应用程序,该应用程序使用HTTP帖子请求将一系列日期(通常一次为7个)上载到CMS后端。我正在使用HttpWebRequest完成此操作。似乎在第一个日期就可以正常工作,但是当它在第二个日期开始时,我得到了System.Net.WebException:请求被中止:请求被取消。

I've been working on developing a middle man application of sorts, which uploads text to a CMS backend using HTTP post requests for a series of dates (usually 7 at a time). I am using HttpWebRequest to accomplish this. It seems to work fine for the first date, but when it starts the second date I get the System.Net.WebException: The request was aborted: The request was canceled.

我到处搜索并找到以下重要线索:

I've searched around and found the following big clues:

http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/0d0afe40-c62a-4089-9d8b -fb4d206434dc

http://www.jaxidian.org/update/2007/05/05/8

http://arnosoftwaredev.blogspot.com/2006/09/net-20-httpwebrequestkeepalive-and.html

他们并没有太大帮助。我试图重载GetWebReuqest,但这没有任何意义,因为我没有使用该函数。

And they haven't been too helpful. I've tried overloading the GetWebReuqest but that doesn't make sense because I don't make any use of that function.

这是我的代码:
http://pastebin.org/115268

I在至少成功运行一次之后,在第245行得到错误。

I get the error on line 245 after it has run successfully at least once.

我将非常感谢我能提供的任何帮助,因为这是我在项目中的最后一步已经工作了一段时间。这是我的第一个C#/ VS项目,因此我乐于接受任何技巧,但我想重点关注如何首先解决此问题。

I'd appreciate any help I can get as this is the last step in a project I've been working on for sometime. This is my first C#/VS project so I'm open to any tips but I would like to focus on getting this problem solved first.

谢谢!

推荐答案

Internet上列出的常见解决方案似乎是将HttpWebRequest的KeepAlive属性设置为false。如果根本原因是该连接即使在一段时间后实际上已自动关闭,但仍有望重新使用,则可以解决此问题。但是,不断打开和关闭连接会对性能造成影响。

The common solution listed on the Internet appears to be to set the KeepAlive property of the HttpWebRequest to false. This can resolve the problem if the root cause is that the connection is expected to be reused even though it was actually closed automatically after some period of time. However, there is a performance hit to constantly opening and closing connections.

遇到此问题时,我使用的另一个可能的解决方案是扩展Timeout属性:WebRequest.ReadWriteTimeout ,WebRequest.Timeout,RequestStream.WriteTimeout和RequestStream.ReadTimeout。请注意,这些时间以毫秒为单位,因此您可能希望超时为1000 * 60 * 10,以表示10分钟(如果您认为这意味着什么,则为600000)。您可以通过减小文件大小来测试这是否更可能是问题的原因。

Another possible solution that I used when I encountered this problem was to extend the Timeout properties: WebRequest.ReadWriteTimeout, WebRequest.Timeout, RequestStream.WriteTimeout, and RequestStream.ReadTimeout. Please note these are in milliseconds, so you may want the timeouts to be 1000 * 60 * 10 to represent 10 minutes (or just 600000 if you think you will know what that means...). You can test if this is the more likely cause of the problem by reducing the file size.

BTW。您的代码已不在列出的网站上。如果仍然存在问题,您可能希望将其包含在帖子的文本中。

BTW. Your code wasn't on the listed website anymore. You may want to include it in the text of your post if it is still an issue.

这篇关于HttpWebRequest:请求被中止:请求被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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