因为各页面编码不同,有的WEB服务器发送编码格式,有的没有发送,导致我下在下来的byte[]转化成string时出错,求能真正转化的代码。很多代码并不能对各种页面下载。(我已经测试过了)解决者另开贴送100分。谢谢。

解决方案 »

  1.   


    #region 取得网页源代码
    /// <summary>
    /// 取得网页源代码
    /// </summary>
    /// <param name="strUrl">要取的URL</param>
    /// <returns>网页源代码</returns>
    public string GetWebCode(string strUrl)
    {
    System.Net.HttpWebRequest httpReq; 
    System.Net.HttpWebResponse httpResp; 
    System.Uri httpURL = new System.Uri(strUrl); 
    httpReq = ((HttpWebRequest)(WebRequest.Create(httpURL))); 
    httpReq.Method = "GET"; 
    httpResp = ((HttpWebResponse)(httpReq.GetResponse())); 
    httpReq.KeepAlive = false; 
    StreamReader reader = new StreamReader(httpResp.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312")); 
    string respHTML = reader.ReadToEnd();
    return respHTML ;
    }
    #endregion
      

  2.   

    有人说http头里面肯定包含编码信息的
    分析就可以了
    我没测试过:)不过最笨的办法
    可以把下载的HTML分析字符串
    里面肯定有编码信息的
      

  3.   

    有的网站头里包含编码信息,有的不包含下载下来HTML,根本解析不出来,怎么找编码?
      

  4.   

    怎么会解析不出来???
    用unicode编码
    大不了文字乱码
    英文怎么会是乱码???
      

  5.   

    你运行一下试试:using System;
    using System.Threading;
    using System.Net;
    using System.IO;public class WorkerThreadExample
    {
        static void Main(string[] args)
        {
            // Create the thread object. This does not start the thread.
    Console.WriteLine(args[0]);
            string html = GetWebCode(args[0]);
    Console.WriteLine(html);
        } #region 取得网页源代码
    /// <summary>
    /// 取得网页源代码
    /// </summary>
    /// <param name="strUrl">要取的URL</param>
    /// <returns>网页源代码</returns>
    public static string GetWebCode(string strUrl)
    {
    System.Net.HttpWebRequest httpReq; 
    System.Net.HttpWebResponse httpResp; 
    System.Uri httpURL = new System.Uri(strUrl); 
    httpReq = ((HttpWebRequest)(WebRequest.Create(httpURL))); 
    httpResp = ((HttpWebResponse)(httpReq.GetResponse())); 
    httpReq.KeepAlive = false; 
    StreamReader reader = new StreamReader(httpResp.GetResponseStream(), System.Text.Encoding.GetEncoding("unicode")); 
    return reader.ReadToEnd();
    }
    #endregion
    }
      

  6.   

    WebClient和System.Text.....具体自己查一下
      

  7.   

    读stream可以用 SreamReader(stream,encoding.utf8)来ReadToEnd 成  string 
    尽管可能是乱码,但是英文是没乱的
    然后查找 charset 的属性关键是这样这个stream就用掉了,要重新抓一个
    当然 如果是utf-8就抓一次就好了我用的是utf-8解码,看来需要统计一下哪种编码比较多...
      

  8.   

    确认一下 WebResponse.Headers 里面带编码的网站很少。解决这个问题只能向IE浏览器学习
    用meta里面的 charset来确定编码
      

  9.   

    Sorry
    我说错了
    应该是UTF8你可以试试
    public static string GetWebCode(string strUrl)
    {
        string s = Encoding.UTF8.GetString(new WebClient().DownloadData(strUrl));
        Console.WriteLine(s);
        return s;
    }
      

  10.   

    谢谢楼上,这个办法还可以,不过如果页面里没有charset怎么办?
      

  11.   

    在meta中没有charset的页面基本上很少吧??
    可以说没有
    呵呵你肯定要try的啊
    如果没有这个页面就忽略掉咯~~~~~~~
      

  12.   

    找到了charset,然后呢?把string再转成byte[],然后再根据得到的编码重新转是吗?能大概给个代码或者伪代码吗?谢谢
      

  13.   

    你说的就对了
    呵呵你在得到远程数据的时候
    把byte[]保存下来
    这样你只要根据编码重新得到一下string就可以了
    没必要重新连接public static string GetWebCode(string strUrl)
    {
        byte[] b = new WebClient().DownloadData(strUrl);
        string charset = FindCharset(Encoding.UTF8.GetString(b));
        return Encoding.GetEncoding(charset).GetString(b);
    }
    private string FindCharset(string str)
    {
        string charset = "";
        //找charset
        return charset;
    }
      

  14.   

    System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();webBrowser.Url  = new uri("http;//www.baidu.com");when webbrowser -> DocumentCompleted
    then text:
    string strHtml  = webbrowser.DocumentText;type:
    webbrowser.DocumentType;up
      

  15.   

    而且一个网站的页面一般只会是一种编码
    所以你可以弄个hashtable之类的保存这个网站的编码
    一个网站只找一次charset
      

  16.   

    WebClient wc = new WebClient();
    string content = wc.DownloadString(url);
      

  17.   

    谢谢 lovefootball(蟑螂(生活就是扯淡--做人要放低姿态)) 我先试试,能加你QQ或者MSN吗?我的QQ:4111852MSN:iuhxq#hotmail.com
      

  18.   

    你还没解决呢?
    去看看 蒋晟 写的代码吧http://community.csdn.net/Expert/topic/5169/5169854.xml?temp=.2164881