我遇到班级HttpWebRequest
的问题
我正在执行脚本的Web服务器(apache / php)中进行请求。
问题是:当请求超过10分钟(这是一个很大的过程)时,我的C#程序没有得到答案,但是当我减少循环(在服务器中)时,C#获得答案。
我在服务器端进行了以下测试:
sleep(10); // 10 seconds
echo "OK";
// In this test everything is ok
但是当我这样做时:
sleep(900) // 15 minutes of wait
echo "OK";
// C# don't get the "ok", even after 20 minutes
我的C#课程:
public class WebBrowser {
private CookieContainer Cookie;
private HttpWebRequest Request;
private string bufferResponse;
public WebBrowser() {
Cookie = new CookieContainer();
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;
System.Net.WebRequest.DefaultWebProxy = null;
}
public CookieContainer GetCookie() {
return Cookie;
}
public string Get( string url ) {
return ExecuteGet( url );
}
protected string ExecuteGet( string url ) {
Request = (HttpWebRequest) HttpWebRequest.Create( url );
Request.Method = "get";
Request.AllowAutoRedirect = false;
Request.CookieContainer = Cookie;
Request.KeepAlive = false;
Request.ContentLength = 0;
Request.Timeout = 20 * ( 1000 * 60 ); // minutos
Request.ReadWriteTimeout = 20 * ( 1000 * 60 ); // minutos
Request.Proxy = null;
Request.ServicePoint.SetTcpKeepAlive( true, 30000, 5000 ); //after 30 seconds, each 5 second
HttpWebResponse Response = (HttpWebResponse) Request.GetResponse();
Stream ResponseStream = Response.GetResponseStream();
StreamReader ResponseReader = new StreamReader( ResponseStream );
bufferResponse = ResponseReader.ReadToEnd();
Response.Close();
ResponseStream.Close();
ResponseReader.Close();
return ( bufferResponse );
}
public string Response() {
return bufferResponse;
}
}
有人可以帮助我吗,我会发疯的。
PS:抱歉英语不好,我是巴西人。