【问题标题】:Second call to HttpWebRequest.GetRequestStream() throws timeout exception第二次调用 HttpWebRequest.GetRequestStream() 抛出超时异常
【发布时间】:2012-07-29 19:26:16
【问题描述】:

帮助...我无法弄清楚为什么 HttpWebRequest.GetRequestStream() 会引发超时 例外......它总是在尝试建立第二个(和后续)时发生 使用 POST 连接。基本上,我正在尝试创建连接 每 30 秒。以下是相关代码:

我的主循环:

for( int i = 0; i < students.Count; i++ ) {

    Log( "Processing: " + students[i].DbID + ";" + students[i].Username + ";" + students[i].Password );

    UserConnection uc = new UserConnection( students[i] );
    uc.ParseAll();


    Log( "Done Processing: " + students[i].DbID + ";" + students[i].Username + ";" + students[i].Password );
}

来自用户连接:

public UserConnection( Student student )
{
    this.student = student;

    this.cookies = new CookieContainer();
    this.InitCookies();

    this.courses = new List<Course>();
}


private void InitCookies()
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create( baseUri );
    req.Method = "GET";
    req.CookieContainer = this.cookies;

    req.GetResponse();


}

public void ParseAll()
{
    ParseCourseInfo(); //get info for each class
    .
    .
    .
}

private void ParseCourseInfo()
{
    HtmlDocument page = GetPage( "POST", homeUri, "username=" + student.Username + "&password=" + student.Password + "&testcookies=1" );
    .
    .
    .
}

GetPage 出现以下异常:

29/07/2012 1:04:22 PM : Exception: System.Net.WebException
Message: The operation has timed out
Source: System
Stack Trace:    at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at URCoursesParserV2.UserConnection.GetPage(String method, Uri pageUri, String queryString) in UserConnection.cs:line 167

这里是问题所在的GetPage代码:

private HtmlDocument GetPage( string method, Uri pageUri, String queryString = "" )
{
    Stream data = null;
    HttpWebResponse res = null;
    HttpWebRequest req = null;
    try {

        req = (HttpWebRequest)WebRequest.Create( pageUri );
        req.CookieContainer = this.cookies;
        req.Timeout = 1000 * 10; //10 seconds - I've also tried leaving it as default
        req.KeepAlive = false; ////I've also tried leaving it as true.

        if( method.ToUpper() == "POST" ) {
            req.Method = "POST";

            if( queryString != "" ) {
                byte[] postBytes = Encoding.UTF8.GetBytes( queryString );
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = postBytes.Length;

                using( data = req.GetRequestStream() ) { //////////////EXCEPTION HERE ON SECOND ITERATION
                    data.Write( postBytes, 0, postBytes.Length );
                    data.Close();
                }
            }

        } else if( method.ToUpper() == "GET" ) {
            req.Method = "GET";

            if( queryString != "" ) {
                pageUri = new Uri( pageUri.ToString() + '?' + queryString );
            }

        } else {
            return null;
        }

        HtmlDocument page = null;
        using( res = (HttpWebResponse)req.GetResponse() ) {
            using( data = res.GetResponseStream() ) {
                page = new HtmlDocument();
                page.Load( data );
            }
        }
        return page;

    } catch(WebException e) {
        URCoursesParser.Log( e );
        return null;

    } catch( Exception e ) {
        URCoursesParser.Log( e );
        return null;

    } finally { ///data and res probably don't need to be checked here now because of 'using' blocks
        if( data != null ) {
            data.Close();
            data = null;
        }
        if( res != null ) {
            res.Close();
            res = null;
        }
        if( req != null ) {
            req.Abort();
            req = null;
        }
    }
}

有什么想法!?!第一次迭代总是好的。如果我终止程序并重新开始第一次迭代就可以了……它总是第二次和后续迭代。

【问题讨论】:

    标签: c# .net post httpwebrequest timeout


    【解决方案1】:

    这可能是问题的一部分:

    private void InitCookies()
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create( baseUri );
        req.Method = "GET";
        req.CookieContainer = this.cookies;
        req.GetResponse();
    }
    

    这里你没有处理响应......所以它没有返回到池的连接。

    目前尚不清楚此方法的用途,但您应该处理响应:

    using (req.GetResponse()) {}
    

    【讨论】:

    • 你是男人!应该为自己节省 4 个小时的时间,然后早点来这里。这里我只需要建立一个连接来初始化一些cookies,这样我就可以在post请求期间登录。
    • @Cailen:如果您稍后在发送“真实”请求时设置它们,它不会初始化cookie吗?也许不是……
    • 谢谢乔恩,真是太棒了。
    • Jon Skeet 再次拯救了这一天
    • 谢谢,乔恩!我已经盯着这段代码两天了!非常感谢。
    【解决方案2】:

    我遇到了类似的问题,但我明确希望在等待响应之前退出该方法。我将请求的超时设置为 100 毫秒,这对我来说似乎解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多