无法设置HttpWebRequest的超时做一个POST时100秒以下高? [英] Can't set HttpWebRequest timeout higher than 100 seconds when doing a POST?

查看:2826
本文介绍了无法设置HttpWebRequest的超时做一个POST时100秒以下高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题做一个帖子的时候,其中的HttpWebRequest不会尊重超过100秒的超时值高。但是,如果请求是GET,100秒以下高的超时值尊重。超时例外是在.GetResponse()调用抛出。我设置的所有超时值,我已经能够发现,但似乎我缺少一个,或者没有在框架中的错误。

I am running into an issue where HttpWebRequest won't respect a timeout value higher than 100 seconds when doing a POST. However, if the request is a GET, a timeout value higher than 100 seconds is respected. The timeout exception is thrown at the .GetResponse() call. I'm setting all the timeout values I have been able to discover but it seems I am missing one, or there is a bug in the framework.

这是一个C#应用程序针对.NET框架3.5,使用Visual Studio 2008的Web服务器IIS 6.0设置为默认的120秒的连接超时而建,保留消息启用...再次GET请求尊重超时值我指定,POST请求尊重超时如果< = 100秒。

This is a C# app targeting the .NET Framework 3.5, built using Visual Studio 2008. The web server is IIS 6.0 with a connection timeout set to the default 120 seconds, keep-alives enabled... again GET requests respect the timeout value I specify, POST requests respect the timeout if <= 100 seconds.

下面是我的代码:

int timeout = 200000; // 200 seconds
HttpWebRequest proxyRequest = (HttpWebRequest)WebRequest.Create(serverUrl);
proxyRequest.Accept = clientRequest.AcceptTypes.ToDelimitedString(", ");
proxyRequest.Method = "POST"
proxyRequest.UserAgent = clientRequest.UserAgent;
proxyRequest.Timeout =  timeout;
proxyRequest.ReadWriteTimeout = timeout;
proxyRequest.KeepAlive = false;
proxyRequest.AllowAutoRedirect = false;
proxyRequest.ServicePoint.Expect100Continue = false;
proxyRequest.ServicePoint.MaxIdleTime = timeout;
proxyRequest.ServicePoint.ConnectionLeaseTimeout = -1;

try
{
    // add post data
    request.ContentType = "application/x-www-form-urlencoded";
    byte[] postData = Encoding.UTF8.GetBytes("somedata=7&moredata=asdf");
    // set some post data
    request.ContentLength = postData.Length;
    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(postData, 0, postData.Length);
        stream.Close();
    }

    // UPDATE
    // don't set Timeout here! It will be ignored
    // proxyRequest.Timeout = timeout;

    // Timeout exception thrown here if GetResponse doesn't return within 100 seconds
    // even though the Timeout value is set to 200 seconds.
    using (HttpWebResponse proxyResponse = (HttpWebResponse)proxyRequest.GetResponse())
    {
        using (Stream stream = proxyResponse.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(stream, Encoding.Default))
            {
                string content = reader.ReadToEnd();
                [other pointless code for this example]
                reader.Close();
            }
            stream.Close();
        }
        proxyResponse.Close();
    }
}
finally
{
    proxyRequest.Abort();
}

当我已超时值设置为5秒,我会收到超时5秒钟后异常正如人们所期望的。这证明超时值不被完全忽略。

When I have set the timeout value to 5 seconds, I will receive a timeout exception after 5 seconds just as one would expect. This proves the Timeout value isn't being completely ignored.

有其他人遇到这个问题?将使用的GetResponse的异步版本来解决这个问题?任何和所有的想法欢迎,我一直停留在这几天。

Has anybody else run into this issue? Will using the Async version of GetResponse get around this issue? Any and all thoughts welcome, I've been stuck on this for a couple days.

更新

我能得到这个职位,尊重超时值,如果我不发布任何数据(这是不是非常有用)。但是,只要我在所有发布的任何数据和CONTENTLENGTH> 0,它timesout在100秒。此外,没有代理参与。

I can get the POST to respect the timeout value if I don't post any data (which isn't very useful). However, as soon as I post any data at all and ContentLength is > 0, it timesout at 100 seconds. Also, no proxies are involved.

更新2

增加了POST数据该例子,在何处不设置超时属性注释

Added the POST data to the example and a comment on where NOT to set the Timeout property

推荐答案

我想通了。这是编码回来,咬我的屁股DRY的一个例子。上面的代码是我真正的代码意译,因此上面的代码将正常工作。

I figured it out. This is an example of DRY coding coming back and biting me in the butt. The code above is a paraphrase of my real code, and as such the code above will work fine.

这个问题是我设置的超时价值,我已经叫后的 proxyRequest.GetRequestStream()添加POST数据。因为我是在同时设置的超时 ReadWriteTimeout 性能,最短的超时获得殊荣。在POST请求的情况下,即使框架让我设定的超时值GetRequestStream一个电话后,就忽略任何值设置(,而是使用默认百秒即使设置它显示了它是经过检查超时属性设置为我的预期)。我想设置超时属性的工作一样设置ReadWriteTimeout属性:如果您尝试设置ReadWriteTimeout属性,您呼吁GetRequestStream后,它抛出一个异常。如果超时做同样的事情,这将有救了我的时间吨。我应该已经抓住了这个越早,但我会记在一个学习的经验。

The issue was I was setting the Timeout value after I had already called proxyRequest.GetRequestStream() to add the POST data. Because I was setting both the Timeout and ReadWriteTimeout properties, the shortest timeout was winning. In the case of a POST request, even though the framework let me set the Timeout value AFTER a call to GetRequestStream, it ignored whatever value was set (and instead used the default 100 seconds even though inspecting the Timeout property after setting it showed it was set to what I expected). I wish setting the Timeout property worked the same as setting the ReadWriteTimeout property: If you attempt to set the ReadWriteTimeout property after you have called GetRequestStream, it throws an exception. If Timeout did the same thing, that would have saved me a TON of time. I should have caught this sooner, but I'll chalk it up to a learning experience.

所以,故事的寓意是:将所有的超时属性为您HttpWebRequest的右当您创建它。

So the moral of the story: Set all the timeout properties for your HttpWebRequest right when you create it.

这篇关于无法设置HttpWebRequest的超时做一个POST时100秒以下高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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