如何找出HtmlAgilityPack.HtmlDocument的ContentType

问题描述 投票:1回答:1

我试图确定HtmlAgility.HtmlDocument的内容类型。任何的想法??

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

我想知道如果可能的话,如何查找hDocument的contentType,或者是否有任何工作。谢谢

html-agility-pack content-type ihtmldocument2
1个回答
0
投票

基本上你想要的是httpwebresponse对象,为了得到这个,你可以像这样使用taskcompletionsource类。

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

我有一段时间没有这样做,并没有机会测试这段代码,但它应该适合你想要的。

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

© www.soinside.com 2019 - 2024. All rights reserved.