How to find out ContentType of HtmlAgilityPack.HtmlDocument

178 views Asked by At

Im trying to determine the content type of HtmlAgility.HtmlDocument. Any idea??

        HtmlWeb web = new HtmlWeb();
        var hDocument = web.Load(/*string*/ url);

I want to know how to find out the contentType of hDocument if possible, or if there is any work round it. Thanks

1

There are 1 answers

0
CodingKuma On BEST ANSWER

Basically what you want is the httpwebresponse object, to get this with hap you can use the taskcompletionsource class like this.

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;

var contentType = httpWebResponse.ContentType

I have not done this in a while and didn't get a chance to test this code but it should work for what you want.

Get HttpWebResponse from Html Agility Pack HtmlWeb https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.contenttype?view=netframework-4.7.2