日期:2014-05-17  浏览次数:21073 次

C# 怎么取网页跳转后的HTML代码
如题譬如一个URL:  http://www.ip138.com/ip2city.asp

直接这个页面的HTML代码为
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>IP地址查询</title>
<meta http-equiv="Refresh" content="0; url=http://iframe.ip138.com/ic.asp" />
</head>
<body>
<script type="text/javascript">
location.href="http://iframe.ip138.com/ic.asp";
</script> 
<a href="http://www.ip138.com/ips138.asp">IP地址所在地查询</a>
</body>
</html>

但是它最终跳转后的代码为
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> 您的IP地址 </title>
</head>
<body style="margin:0px"><center>您的IP是:[27.113.53.104] 来自:韩国</center></body></html>


找了好多C#访问网页的代码,如这类
WebClient wc = new WebClient();
            return wc.DownloadString(url);


都是只能获取上面的结果,而取不到像浏览品一样最终跳转后的下面的结果。
还望高人指点迷津,谢谢!!!





------最佳解决方案--------------------
这不是跳转,而是js脚本渲染的。
如果就是这个网站,你抓http://iframe.ip138.com/ic.asp好了
如果是要通用的,则相当于需要一个javascript引擎了,最简单的实现是借助webbrowser。
------其他解决方案--------------------

            System.Net.CookieContainer cookies = new System.Net.CookieContainer();
            //HttpHelper.GetHtml("http://www.ip138.com/ip2city.asp", cookies);
            //HttpHelper.GetHtml("http://city.ip138.com/ip2city.asp", cookies);
            Console.WriteLine(HttpHelper.GetHtml("http://iframe.ip138.com/ic.asp", cookies));

其实这个地址跳了3次,
只取最后一次的就可以了。
------其他解决方案--------------------
看来是我在概念上犯了错误,谢谢了!