【问题标题】:How to find out ContentType of HtmlAgilityPack.HtmlDocument如何找出 HtmlAgilityPack.HtmlDocument 的 ContentType
【发布时间】:2019-03-15 15:24:16
【问题描述】:

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

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

如果可能,我想知道如何找出 hDocument 的 contentType,或者是否有任何解决方法。谢谢

【问题讨论】:

    标签: html-agility-pack content-type ihtmldocument2


    【解决方案1】:

    基本上你想要的是 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-17
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 2013-01-05
      • 2011-11-02
      • 1970-01-01
      相关资源
      最近更新 更多