从Html Agility Pack HtmlWeb获取HttpWebResponse

c# html-agility-pack httpwebresponse

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

如同,如果我打电话:

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

有没有一种方便的方法从web实例检索HttpWebResponse来查看标题 - 比如检索内容类型等标题属性?

有前后代表,它们将起作用:

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();

一般承认的答案

您可以使用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;


Related

许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow