您需要PostRequestHandler
在HtmlWeb
实例上注册一个事件,它将在每个下载的文档之后引发,您将可以访问该HttpWebResponse
对象.它有一个属性StatusCode
.
HtmlWeb web = new HtmlWeb(); HttpStatusCode statusCode = HttpStatusCode.OK; web.PostRequestHandler += (request, response) => { if (response != null) { statusCode = response.StatusCode; } } var doc = web.Load(completeUrl) if (statusCode == HttpStatusCode.OK) { // received a read document }
看一下GutHub上HtmlAgilityPack的代码,它更简单,HtmlWeb
有一个属性StatusCode
设置值:
var web = new HtmlWeb(); var document = web.Load(completeurl); if (web.StatusCode == HttpStatusCode.OK) { var urls = document.DocumentNode.Descendants("img") .Select(e => e.GetAttributeValue("src", null)) .Where(s => !String.IsNullOrEmpty(s)).ToList(); }
AgilityPack API已有更新.诀窍仍然是一样的:
var htmlWeb = new HtmlWeb(); var lastStatusCode = HttpStatusCode.OK; htmlWeb.PostResponse = (request, response) => { if (response != null) { lastStatusCode = response.StatusCode; } };