zoukankan      html  css  js  c++  java
  • ASP.NET获取HTML代码时的乱码问题


            
    //
            
    //方法GetPageSource:获取指定网页的HTML代码
            
    //
            public static string GetPageSource(string URL)  
            

                Uri uri 
    =new Uri(URL); 

                HttpWebRequest hwReq 
    = (HttpWebRequest)WebRequest.Create(uri); 
                HttpWebResponse hwRes 
    = (HttpWebResponse)hwReq.GetResponse(); 

                hwReq.Method 
    = "Get"
                hwReq.KeepAlive 
    = false
                
    //将该属性设置为 true 以发送带有 Keep-alive 值的 Connection HTTP 标头。
                
    //应用程序使用 KeepAlive 指示持久连接的首选项。
                
    //当 KeepAlive 属性为 true 时,应用程序与支持它们的服务器建立持久连接。
                
    //注意   使用 HTTP/1.1 时,Keep-Alive 默认情况下处于打开状态。
                
    //将 KeepAlive 设置为假可能导致将 Connection: Close 标头发送到服务器。

                StreamReader reader
    = new StreamReader(hwRes.GetResponseStream(),System.Text.Encoding.GetEncoding("gb2312"));

                
                
    return reader.ReadToEnd(); 
            }
    我写了这个方法从指定页面中获取HTML代码。
    但当指定页面是UTF-8时,中文即出现乱码。
    有什么办法知道指定页面使用的是什么编码?
    如果指定页面本来就使用GB2312,我又把它转换为GB2312,会出现什么结果?
  • 相关阅读:
    【Rust】多种错误类型
    【Rust】Result别名
    【Rust】Option然后
    【Rust】可选和错误
    【Rust】Result问号
    【Rust】Option转换
    【Rust】Option展开
    【Rust】Result结果
    【Rust】Result提前返回
    jQuery过滤 安静点
  • 原文地址:https://www.cnblogs.com/bankey/p/806869.html
Copyright © 2011-2022 走看看