我正在按照您的意图使用 HttpWebRequest:在我完成响应后处理响应,希望这会使其重用任何可用的 TCP 连接,但事实并非如此:它在之后关闭连接收到响应。我在使用 TCPView 时看到了这种情况。
这是我的 HttpWebRequest 代码:
private HttpWebResponse ExecuteRequest(String baseRelativeUri, String method, Ds postValues) {
/////////////////////////////////////////
// Set-up
Uri uri = new Uri( _baseUri, baseRelativeUri );
_cookies.ProcessDomains();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( uri );
request.CookieContainer = _cookies;
request.Method = method;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";
if( postValues != null ) SetPostContent( request, postValues );
/////////////////////////////////////////
// Response
return (HttpWebResponse)request.GetResponse();
}
protected HtmlDocument ExecuteRequestHtml(String baseRelativeUri, String method, Ds postValues, HttpStatusCode expectedStatusCode) {
using(HttpWebResponse response = ExecuteRequest(baseRelativeUri, method, postValues)) {
if( response.StatusCode != expectedStatusCode ) throw new WebException("Did not receive " + expectedStatusCode + " response.");
/////////////////////////////////////////
// HtmlDocument
using(Stream stream = response.GetResponseStream()) {
HtmlDocument doc = new HtmlDocument();
doc.Load( stream );
return doc;
}
}
}
根据 MSDN,HttpWebRequest.KeepAlive 属性默认为 true,HttpWebRequest.Pipelines 也是如此。那么我做错了什么?
谢谢!