CookieContainer的HttpWebRequest操作超时 [英] HttpWebRequest Operation Timeout with CookieContainer

查看:84
本文介绍了CookieContainer的HttpWebRequest操作超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用HttpWebRequest并添加以下内容时:

When I am using HttpWebRequest and add this:

CookieContainer container = new CookieContainer();
request.CookieContainer = container;

它将在

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

如果我不添加CookieContainer,则程序运行无错误.为什么这行不通?

If I don't add the CookieContainer the program runs without errors. Why doesn't this work?

推荐答案

如何创建请求对象?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
            request.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();

CookieContainer

尝试设置 myWebRequest.Timeout = xxx

  • 如果您正在运行多个并发请求,请尝试设置连接限制
  • System.Net.ServicePointManager.DefaultConnectionLimit
    ServicePoint.ConnectionLimit

    System.Net.ServicePointManager.DefaultConnectionLimit
    ServicePoint.ConnectionLimit

    检查这些链接以获取更多信息:

    Check these links for more information:

    HttpWebRequest操作已超时"
    我需要帮助来设置.NET HttpWebRequest超时
    在C#中调整HttpWebRequest连接超时

    HttpWebRequest "operation has timed out"
    I need help setting .NET HttpWebRequest timeout
    Adjusting HttpWebRequest Connection Timeout in C#

    有很多原因导致GetResponse()可能挂起.一些是:

    here are many reasons why GetResponse() might hang. Some are:

    1)您已达到客户端上的连接限制(每个http/1.1服务器2个conns,每个http/1.0服务器4个连接或在ServicePoint上设置的自定义限制),并且没有连接可以自由地将请求发送到服务器.换句话说,您可能有2个未完成的请求(例如:发送至1.1服务器),这些请求花费了2分钟以上的时间,然后再向同一服务器发出另一个GetResponse().最后一个getresponse()可能会超时.

    1) You have reached the connection limit on the client ( 2 conns per http/1.1 server, 4 connections per http/1.0 server, or custom limit set on the ServicePoint), and no connection is free to send a request to the server. In other words, you might have 2 requests outstanding (eg: to a 1.1 server) which are taking more than 2 minutes to complete, and then issue another GetResponse() to the same server. The last getresponse() might time out.

    2)您尚未关闭较早的httpwebresponse的响应流.因此,即使连接是免费的,请求也不会发送.

    2) You have not closed the response stream of an earlier httpwebresponse. So, even if a connection is free, the request wont be sent.

    3)服务器花费的时间太长,无法发送响应.

    3) The server is taking too long to send the response.

    这篇关于CookieContainer的HttpWebRequest操作超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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