gpt4 book ai didi

c# - 第二次调用 HttpWebRequest.GetRequestStream() 抛出超时异常

转载 作者:太空狗 更新时间:2023-10-29 22:03:58 25 4
gpt4 key购买 nike

帮助...我不明白为什么 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;
}
}
}

任何想法!?!第一次迭代总是好的。如果我终止程序并重新启动第一次迭代又没问题...它始终是第二次和后续迭代。

最佳答案

这可能是问题的一部分:

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

在这里您没有处理响应...所以它没有将连接返回到池中。

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

using (req.GetResponse()) {}

关于c# - 第二次调用 HttpWebRequest.GetRequestStream() 抛出超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com