c#获取页面html源代码,但是得到的结果和我直接右键查看源文件看到的代码不一样? c#获取页面html源代码,但是得到的结果和我直接右键查看源文件看到的代码不一样?这是什么原因呢?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 来了/// <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; } 还有一点,有的页面是没问题的,但是有些页面就有问题了!我用的使这个页面地址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)” 如果不一样的是乱码的话,可以试一下reader = new StreamReader(stream, Encoding.UTF8);可能是编码方式不一样吧,你换一个试一下,用Default看看 不一样的地方不是有乱码,里面的内容都很正常,但是和我在页面上右键查看源代码得到的代码不一样!这是什么原因?搞不懂了!有人说可能是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; 你看看页面是不是有iframe,或者js生成的内容 不一样在 汉字上,汉字会变乱码, 其他的代码都正常。<!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>这个是生成前的。。 UTF-8 的网页要用 UTF-8 编码去读, GB2312 的要用 GB2312 编码去读. 否则肯定乱码啊. vs 2008 自带的水晶报表有时间或是次数的限制吗? 大家帮我看看,一段C#程序是什么意思 大家帮忙看看这道题目 如何提高远程打开文件夹和文件的速度??? 怎么实现自动跳出查询结果 如何获得TreeView控件中所有节点的个数? 关于BerkeleyDB在C#中的异常问题 Legend代表什么意思 在线给分 奇怪,Socket——所有端口都能连上 如何储存SQL连接字符串 如何通过关闭子窗体进而结束应用程序
/// <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;
}
我用的使这个页面地址
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)”
reader = new StreamReader(stream, Encoding.UTF8);
可能是编码方式不一样吧,你换一个试一下,用Default看看
有人说可能是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;
<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>这个是生成前的。。