Get HttpWebResponse from Html Agility Pack HtmlWeb

c# html-agility-pack httpwebresponse

Question

Can HttpWebResponse be obtained from Html Agility Pack HtmlWeb?

As in, if I call:

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

Is there a convenient method to retrieve the HttpWebResponse from the web instance to view headers - such as to retrieve header properties like content type?

There are pre and post delegates, which will work:

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/");

Instead, is there a way to get the response something like: (HttpWebResponse)web.GetResponse();

Accepted Answer

You can use 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;



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why