zoukankan      html  css  js  c++  java
  • HtmlWeb类

      HtmlWeb类是一个从网络上获取一个HTML文档的类,其提供的功能大多是基于完成此需求出发。现在来来HtmlWeb类有哪些方法以及属性。

      一、属性

    bool AutoDetectEncoding { get; set; }     获取或设置一个值,该值指示是否必须将自动检测文档编码。
    bool CacheOnly { get; set; }          获取或设置一个值,该值指示是否只从缓存中获取的文档。如果此设置为 true 并且文档未找到在缓存中,并不会加载。
    string CachePath { get; set; }          获取或设置缓存路径。如果为 null,则将使用无缓存的机制。
    bool FromCache { get; }            获取一个值,该值指示是否从缓存中检索的最后一次加载的文档。
    Encoding OverrideEncoding { get; set; }    获取或设置用于重写从任何 web 请求的响应流的编码。
    int RequestDuration { get; }           获取上次请求持续时间,以毫秒为单位。
    Uri ResponseUri { get; }            获取的互联网资源的实际响应请求的 URI。
    HttpStatusCode StatusCode { get; }      获取上次请求状态。
    int StreamBufferSize { get; set; }       获取或设置用于内存操作的缓冲区的大小。
    bool UseCookies { get; set; }          获取或设置一个值,该值指示是否将存储的 cookie。
    string UserAgent { get; set; }         获取或设置任何 webrequest 上发送的用户代理 HTTP 1.1 标头
    bool UsingCache { get; set; }          获取或设置一个值,指示是否使用的缓存机制。

    二、方法(删减了不少重载)

    object CreateInstance(string url, Type type);   从指定的互联网资源创建给定类型的实例。
    void Get(string url, string path);          从互联网资源获取 HTML 文档并将其保存到指定的文件。
    string GetCachePath(Uri uri);            获取指定的 url 缓存文件路径。
    static string GetContentTypeForExtension(string extension, string def);      获取给定的路径扩展的 MIME 内容类型。
    static string GetExtensionForContentType(string contentType, string def);      获取给定的 MIME 内容类型的路径扩展。
    HtmlDocument Load(string url);          从一个网址加载代码并返回一个HtmlDocument
    void LoadHtmlAsXml(string htmlUrl, string xsltUrl, XsltArgumentList xsltArgs, XmlTextWriter writer);   从互联网资源加载 HTML 文档,并将其保存到指定的 XmlTextWriter。

    代码示例:由于对HTTP方面的很多知识尚不熟悉,因此先跳过,以后再补充那些不懂的。

     static void Main(string[] args)
            {
                HtmlWeb web = new HtmlWeb();
                Console.WriteLine(web.AutoDetectEncoding);  //输出 True
                Console.WriteLine(web.CacheOnly);   //输出 False   
                Console.WriteLine(web.CachePath);   //输出 空白(啥都不输出)
                Console.WriteLine(web.OverrideEncoding);    //输出  空白
                Console.WriteLine(web.RequestDuration);     //输出 0 上次持续请求时间为0?
                Console.WriteLine(web.ResponseUri);         //输出 空白
                Console.WriteLine(web.StatusCode);          //输出 Ok 就是 200了,枚举来的
                Console.WriteLine(web.StreamBufferSize);    //输出 1024
                Console.WriteLine(web.UseCookies);          //输出 False
                Console.WriteLine(web.UserAgent);           //输出 FireFox...................................
                Console.WriteLine(web.UsingCache);          //输出 False
                HtmlDocument doc = web.Load("http://www.juedui100.com");
                Console.WriteLine(doc.DocumentNode.SelectSingleNode("//title").InnerText);  //输出 交友_征婚_找对象,上绝对100婚恋交友网
    
                Uri uri = new Uri("http://www.juedui100.com");
    
                web.CachePath = @"D:\juedui100\";
                web.UsingCache = true;      //要先开启使用缓存,下面的方法才能够使用
                Console.WriteLine(web.GetCachePath(uri));
    
                web.Get("http://www.juedui100.com",@"D:\juedui100.html"); 
                //二、方法(删减了不少重载)
    
                //object CreateInstance(string url, Type type);   从指定的互联网资源创建给定类型的实例。
                //static string GetContentTypeForExtension(string extension, string def);            获取给定的路径扩展的 MIME 内容类型。 
                //static string GetExtensionForContentType(string contentType, string def);            获取给定的 MIME 内容类型的路径扩展。
                //void LoadHtmlAsXml(string htmlUrl, string xsltUrl, XsltArgumentList xsltArgs, XmlTextWriter writer);      从互联网资源加载 HTML 文档,并将其保存到指定的 XmlTextWriter。
    
                Console.ReadKey();
            }
  • 相关阅读:
    10月9日学习日志
    10月2日学习日志
    11月3日学习日志
    10月5日学习日志
    10月6日学习日志
    10月7日学习日志
    11月4日学习日志
    AccessDatabaseEngine.exe 32位64安装失败问题
    国产各安卓系统接收消息在杀进程后
    SAP容差组
  • 原文地址:https://www.cnblogs.com/kissdodog/p/2938698.html
Copyright © 2011-2022 走看看