如何超时使用HTML敏捷性包的请求 [英] How to Timeout a request using Html Agility Pack
问题描述
我正在做一个请求到远程Web服务器当前处于脱机状态是(故意的)。
我想弄清楚超时请求的最佳途径。基本上,如果请求运行时间超过X毫秒,然后退出请求,并返回一个空
响应。
目前的Web请求只是坐在那里等待响应......
我将如何最好的办法这个问题?
下面是一个当前code段
公开的Json presult关于(字符串HomePageUrl)
{
Models.Pocos.About约= NULL;
如果(HomePageUrl.RemoteFileExists())
{
//使用HTML敏捷性包,我们只想要提取
从远程页面//相应的数据。
HtmlWeb HW =新HtmlWeb();
的HTMLDocument DOC = hw.Load(HomePageUrl);
HtmlNode节点= doc.DocumentNode.SelectSingleNode(// DIV [@类='wrapper1界']);
如果(节点!= NULL)
{
关于=新Models.Pocos.About {HTML = node.InnerHtml};
}
// TODO:看看这个else语句是否必要
其他
{
约= NULL;
}
}
返回this.Jsonp(约);
}
我不得不做出一个小的调整,我的原贴code
公开的Json presult关于(字符串HomePageUrl)
{
Models.Pocos.About约= NULL;
// *************零钱 - 增加了超时以毫秒为单位来RemoteFileExists扩展方法。
如果(HomePageUrl.RemoteFileExists(1000))
{
//使用HTML敏捷性包,我们只想要提取
从远程页面//相应的数据。
HtmlWeb HW =新HtmlWeb();
的HTMLDocument DOC = hw.Load(HomePageUrl);
HtmlNode节点= doc.DocumentNode.SelectSingleNode(// DIV [@类='wrapper1界']);
如果(节点!= NULL)
{
关于=新Models.Pocos.About {HTML = node.InnerHtml};
}
// TODO:看看这个else语句是否必要
其他
{
约= NULL;
}
}
返回this.Jsonp(约);
}
然后我修改 RemoteFileExists
扩展方法有一个超时
公共静态布尔RemoteFileExists(这个字符串URL,INT超时)
{
尝试
{
//创建的HttpWebRequest
HttpWebRequest的要求= WebRequest.Create(URL)为HttpWebRequest的;
// ************这里补充
//超时x毫秒之后请求
request.Timeout =超时;
// ************
//设置请求方法HEAD,你也可以使用太。
request.Method =HEAD;
//获取网站的响应。
HttpWebResponse响应= request.GetResponse()作为HttpWebResponse;
//返回true,如果状态code == 200
返程(response.Status code ==的HTTPStatus code.OK);
}
抓住
{
//任何异常都将返回false。
返回false;
}
}
在此方法中,如果之前 RemoteFileExists
我超时火灾可以确定头的响应,那么我的布尔
返回假的。
I'm making a request to a remote web server that is currently offline (on purpose).
I'd like to figure out the best way to time out the request. Basically if the request runs longer than "X" milliseconds, then exit the request and return a null
response.
Currently the web request just sits there waiting for a response.....
How would I best approach this problem?
Here's a current code snippet
public JsonpResult About(string HomePageUrl)
{
Models.Pocos.About about = null;
if (HomePageUrl.RemoteFileExists())
{
// Using the Html Agility Pack, we want to extract only the
// appropriate data from the remote page.
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(HomePageUrl);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='wrapper1-border']");
if (node != null)
{
about = new Models.Pocos.About { html = node.InnerHtml };
}
//todo: look into whether this else statement is necessary
else
{
about = null;
}
}
return this.Jsonp(about);
}
I had to make a small adjustment to my originally posted code
public JsonpResult About(string HomePageUrl)
{
Models.Pocos.About about = null;
// ************* CHANGE HERE - added "timeout in milliseconds" to RemoteFileExists extension method.
if (HomePageUrl.RemoteFileExists(1000))
{
// Using the Html Agility Pack, we want to extract only the
// appropriate data from the remote page.
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(HomePageUrl);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='wrapper1-border']");
if (node != null)
{
about = new Models.Pocos.About { html = node.InnerHtml };
}
//todo: look into whether this else statement is necessary
else
{
about = null;
}
}
return this.Jsonp(about);
}
Then I modified my RemoteFileExists
extension method to have a timeout
public static bool RemoteFileExists(this string url, int timeout)
{
try
{
//Creating the HttpWebRequest
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
// ************ ADDED HERE
// timeout the request after x milliseconds
request.Timeout = timeout;
// ************
//Setting the Request method HEAD, you can also use GET too.
request.Method = "HEAD";
//Getting the Web Response.
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//Returns TRUE if the Status code == 200
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
//Any exception will returns false.
return false;
}
}
In this approach, if my timeout fires before RemoteFileExists
can determine the header response, then my bool
will return false.
这篇关于如何超时使用HTML敏捷性包的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!