HttpWebResonse挂在多个请求上 [英] HttpWebResonse hangs on multiple request
问题描述
我有一个创建许多Web请求以下载网站新闻页面的应用程序 (我已经测试了许多网站) 一段时间后,我发现应用程序在获取html源时速度变慢,然后我发现HttpWebResonse无法获取响应.我只发布完成这项工作的功能.
I've an application that create many web request to donwload the news pages of a web site (i've tested for many web sites) after a while I find out that the application slows down in fetching the html source then I found out that HttpWebResonse fails getting the response. I post only the function that do this job.
public PageFetchResult Fetch()
{
PageFetchResult fetchResult = new PageFetchResult();
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URLAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Uri requestedURI = new Uri(URLAddress);
Uri responseURI = resp.ResponseUri;
if (Uri.Equals(requestedURI, responseURI))
{
string resultHTML = "";
byte[] reqHTML = ResponseAsBytes(resp);
if (!string.IsNullOrEmpty(FetchingEncoding))
resultHTML = Encoding.GetEncoding(FetchingEncoding).GetString(reqHTML);
else if (!string.IsNullOrEmpty(resp.CharacterSet))
resultHTML = Encoding.GetEncoding(resp.CharacterSet).GetString(reqHTML);
resp.Close();
fetchResult.IsOK = true;
fetchResult.ResultHTML = resultHTML;
}
else
{
URLAddress = responseURI.AbsoluteUri;
relayPageCount++;
if (relayPageCount > 5)
{
fetchResult.IsOK = false;
fetchResult.ErrorMessage = "Maximum page redirection occured.";
return fetchResult;
}
return Fetch();
}
}
catch (Exception ex)
{
fetchResult.IsOK = false;
fetchResult.ErrorMessage = ex.Message;
}
return fetchResult;
}
任何解决方案将不胜感激
any solution would greatly appreciate
推荐答案
我同意@volody,而且HttpWebRequest也已经具有称为MaximumAutomaticRedirections的属性,该属性设置为50,可以将其设置为5以自动实现所需的外观因为无论如何在此代码中,它都会引发异常,并将由您的代码处理.
I agree with @volody, Also HttpWebRequest already have property called MaximumAutomaticRedirections, which is set to 50, you can set it to 5 to automatically achieve what you are looking for in this code anyway, it will raise exception and that will be handled by your code.
只需设置
request.MaximumAutomaticRedirections = 5;
这篇关于HttpWebResonse挂在多个请求上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!