Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';在执行POST时,是否将HttpWebRequest超时设置为大于100秒?_C#_Httpwebrequest_Timeout_Http Post - Fatal编程技术网

C# Can';在执行POST时,是否将HttpWebRequest超时设置为大于100秒?

C# Can';在执行POST时,是否将HttpWebRequest超时设置为大于100秒?,c#,httpwebrequest,timeout,http-post,C#,Httpwebrequest,Timeout,Http Post,我遇到了一个问题,HttpWebRequest在执行POST时不会遵守超过100秒的超时值。但是,如果请求是GET,则会考虑大于100秒的超时值。在.GetResponse()调用时引发超时异常。我正在设置我能够发现的所有超时值,但似乎我缺少了一个,或者框架中有一个bug 这是一款针对.NETFramework3.5的C#应用程序,使用VisualStudio2008构建。web服务器是IIS 6.0,连接超时设置为默认的120秒,保持激活状态。。。同样,GET请求遵守我指定的超时值,POST请

我遇到了一个问题,HttpWebRequest在执行POST时不会遵守超过100秒的超时值。但是,如果请求是GET,则会考虑大于100秒的超时值。在.GetResponse()调用时引发超时异常。我正在设置我能够发现的所有超时值,但似乎我缺少了一个,或者框架中有一个bug

这是一款针对.NETFramework3.5的C#应用程序,使用VisualStudio2008构建。web服务器是IIS 6.0,连接超时设置为默认的120秒,保持激活状态。。。同样,GET请求遵守我指定的超时值,POST请求遵守超时值。如果为0,则超时时间为100秒。此外,不涉及任何代理

更新2


将POST数据添加到示例中,并添加了关于不在何处设置超时属性的注释

在客户端和服务器之间是否有web代理?也许这是在使用超时本身


我建议您使用查看网络级别上发生了什么,特别是在100秒后网络上是否发生了任何事情。

我找到了答案。这是一个干编码回来咬我屁股的例子。上面的代码是对我真实代码的一种解释,因此上面的代码可以很好地工作

问题是我在调用了proxyRequest.GetRequestStream()以添加POST数据之后设置了超时值。因为我同时设置了超时读写超时属性,所以最短的超时是成功的。在POST请求的情况下,尽管框架允许我在调用GetRequestStream后设置超时值,但它忽略了设置的任何值(而是使用默认的100秒,尽管设置后检查Timeout属性显示它设置为我期望的值)。我希望设置Timeout属性的工作原理与设置ReadWriteTimeout属性的工作原理相同:如果在调用GetRequestStream后尝试设置ReadWriteTimeout属性,它会引发异常。如果Timeout做了同样的事情,那会节省我很多时间。我本应该早点发现这一点,但我会把它记为一次学习经历


因此,这个故事的寓意是:在创建HttpWebRequest时,为它设置所有超时属性。

不涉及代理,只涉及直接连接。我不完全确定如何解释wireshark中的数据,但使用MS Network Monitor 3.4,我看到在POST请求后100秒,有一个数据包从我的盒子发送到Web服务器,带有标志:…a…F-F是数据的结尾。对我来说,这排除了Web服务器是罪魁祸首的可能性。@thein:听起来确实如此。post请求本身是否存在与超时相关的内容?(我没料到会这样。)谢谢你的帮助-我在这个过程中设置Timeout属性太晚了。我希望设置超时会像ReadWriteTimeout那样引发异常…6年后,在阅读您的解决方案之前,我花了几个小时处理相同的问题。非常感谢。
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();
}