c#获取页面html源代码,但是得到的结果和我直接右键查看源文件看到的代码不一样?
这是什么原因呢?
谢谢!

解决方案 »

  1.   

    来了
    /// <summary>
            /// 获得页面的html代码
            /// </summary>
            /// <param name="url">页面地址</param>
            protected string getHtml(string url)
            {
                string html = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Accept = "*/*";
                HttpWebResponse response = null;
                Stream stream = null;
                StreamReader reader = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                    stream = response.GetResponseStream();
                    reader = new StreamReader(stream, Encoding.UTF8);
                    html = reader.ReadToEnd().Replace("\r\n", "");   //我知道这里会改变html代码,但和这里没关系
                }/*
                catch (Exception excpt)
                {
                    Console.WriteLine(excpt);
                    Console.Write("\n【注意】出现异常,输入任意字符和回车继续:");
                    Console.ReadLine();
                }*/
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        reader.Dispose();
                    }
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }
                }
                return html;
            }
      

  2.   

    还有一点,有的页面是没问题的,但是有些页面就有问题了!
    我用的使这个页面地址
    string url = "http://zh.wikipedia.org/w/index.php?title=Wikipedia:%E5%88%86%E9%A1%9E%E7%B4%A2%E5%BC%95&oldid=8997863";麻烦哪位运行一下看看结果。
    有可能是需要设置header。
    我用的是ie7,但是安装了HttpAnalyzerStdV4后看到的User-Agent是“Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; InfoPath.2)”
      

  3.   

    如果不一样的是乱码的话,可以试一下
    reader = new StreamReader(stream, Encoding.UTF8);
    可能是编码方式不一样吧,你换一个试一下,用Default看看
      

  4.   

    不一样的地方不是有乱码,里面的内容都很正常,但是和我在页面上右键查看源代码得到的代码不一样!这是什么原因?搞不懂了!
    有人说可能是header的关系,但是我把能加上去的header内容都上去了,但是还是一样的效果!
                request.Accept = "*/*";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; InfoPath.2)";
                request.Headers["Accept-Language"] = "zh-cn";
                request.Referer = url;
      

  5.   

    你看看页面是不是有iframe,或者js生成的内容
      

  6.   

    不一样在 汉字上,汉字会变乱码, 其他的代码都正常。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>
    鏍囬
    </title><meta http-equiv="expires" content="0" /><meta name="keywords" content="1" /><meta name="description" content="2" /><meta name="author" content="3" /><meta name="Copyright" content="4" /><link href="css/main.css" rel="stylesheet" type="text/css" /><link href="css/style.css" rel="stylesheet" type="text/css" />
        <script language="javascript" type="text/javascript">
            function tiaozhuan()
            {
                window.open("Gonggao.aspx","_self");
            }
        </script> 
    </head>
    <body>这个是生成以后的
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>
    标题
    </title><meta http-equiv="expires" content="0" /><meta name="keywords" content="1" /><meta name="description" content="2" /><meta name="author" content="3" /><meta name="Copyright" content="4" /><link href="css/main.css" rel="stylesheet" type="text/css" /><link href="css/style.css" rel="stylesheet" type="text/css" />
        <script language="javascript" type="text/javascript">
            function tiaozhuan()
            {
                window.open("Gonggao.aspx","_self");
            }
        </script> 
    </head>这个是生成前的。。
      

  7.   

    UTF-8 的网页要用 UTF-8 编码去读, GB2312 的要用 GB2312 编码去读. 否则肯定乱码啊.