从Html Agility Pack HtmlWeb获取HttpWebResponse

时间:2014-08-31 20:49:07

标签: c# html-agility-pack httpwebresponse

可以从Html Agility Pack HttpWebResponse获取HtmlWeb吗?

如同,如果我打电话:

HtmlWeb web = new HtmlWeb ();
HtmlDocument document = web.Load ("http://stackoverflow.com/");

是否有方便的方法从HttpWebResponse实例中检索web以查看标题 - 例如检索内容类型等标题属性?

有前任和后任代表,可以工作:

HtmlWeb web = new HtmlWeb ();

web.PreRequest = delegate (HttpWebRequest request) {
    request.Timeout = 10 * 1000;
    return true;
};
web.PostResponse = delegate (HttpWebRequest request, HttpWebResponse response) {
    //response.Headers;
    //response.ContentType;
};

HtmlDocument document = web.Load ("http://stackoverflow.com/");

相反,有没有办法让响应像:(HttpWebResponse)web.GetResponse();

1 个答案:

答案 0 :(得分:3)

您可以使用TaskCompletionSource + async / await

var web = new HtmlAgilityPack.HtmlWeb();
var tcs = new TaskCompletionSource<HttpWebResponse>();

web.PostResponse = delegate(HttpWebRequest request, HttpWebResponse response)
{
    tcs.SetResult(response);
};

var  document = web.Load("http://stackoverflow.com/");
var httpWebResponse = await tcs.Task;