| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5573 人关注过本帖
标题:C#无法正确获取网站页面的源码
只看楼主 加入收藏
福宝相安
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-2-28
收藏
 问题点数:0 回复次数:3 
C#无法正确获取网站页面的源码
url:http://www.
我的代码:
        public static string GetPage(string url)
        {
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                StreamReader reader = null;
                try
                {
                        request = (HttpWebRequest)WebRequest.Create(url)
                        request.Timeout = 30000;
                        request.Method = "GET";
                        request.KeepAlive = false;
                        request.AllowAutoRedirect = false;
                        response = (HttpWebResponse)request.GetResponse();
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                                reader = new StreamReader(response.GetResponseStream(), true);
                                string html = reader.ReadToEnd();
                                return html;
                        }
                }
                catch { }
                finally
                {
                        if (response != null)
                        {
                                response.Close();
                                response = null;
                        }
                        if (reader != null)
                                reader.Close();
                        if (request != null)
                                request = null;
                }
                return string.Empty;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
                TextBox1.Text = GetPage("http://www.);
        }
获取到的部分源码:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Welcome</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <script>
            var l7_scheme = 'http';
            var l7_port = '80';
            var l7_action_url = "http://resources.
            var l7_vcode_img = 'http://resources.';
            var l7_domain_img = 'default';
            l7_domain_img = "*.
l7_scheme = "http";

            l7_domain_img = l7_scheme + '://resources.'+ l7_domain_img + '_' + l7_port + '.jpg';
            document.write("<script src='"+ l7_scheme +"://resources.'></script><link href='"+ l7_scheme +"://resources.' rel='stylesheet' type='text/css'>");
在浏览中查看到的部分源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<HTML xmlns="http://www. xmlns:esun>
<HEAD><TITLE>EK娱乐平台  - 历史号码走势  </TITLE>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META http-equiv="Pragma" content="no-cache" />
<link href="http://upstaticcache.b0. rel="stylesheet" type="text/css" />
<link href="http://upstaticcache.b0. rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="http://upstaticcache.b0.
<script language="javascript" type="text/javascript" src="http://upstaticcache.b0.
<script language="javascript" type="text/javascript" src="http://upstaticcache.b0.
<script language="javascript" type="text/javascript" src="http://upstaticcache.b0.
<link rel="stylesheet" href="http://upstaticcache.b0. type="text/css" />
<script language="javascript" type="text/javascript" src="http://upstaticcache.b0.
<script language="javascript" src="http://upstaticcache.b0.
<script language="javascript" src="http://upstaticcache.b0.
<script language="javascript" src="http://upstaticcache.b0.
<link rel="stylesheet" href="http://upstaticcache.b0. type="text/css" />
</HEAD>
<BODY style="background: url(http://upstaticcache.b0.) repeat-x center top; margin:10px;height:100%;">
<div id="right_01">
<div class="right_01_01"><SPAN class="action-span1"><A
href="./" target='_top'>高频游戏</a>  - 历史号码走势 </SPAN></div>

</div>
<script language="javascript">
fw.onReady(function(){
    Chart.init();   
    DrawLine.bind("chartsTable","has_line");

        DrawLine.color('#499495');
    DrawLine.add((parseInt(0)*11+5+1),2,11,0);
        DrawLine.color('#E4A8A8');
    DrawLine.add((parseInt(1)*11+5+1),2,11,0);
        DrawLine.color('#499495');
    DrawLine.add((parseInt(2)*11+5+1),2,11,0);
        DrawLine.color('#E4A8A8');
为什么会这样,请各位高手指点下!
搜索更多相关主题的帖子: request public null 网站 
2016-02-28 04:11
福宝相安
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-2-28
收藏
得分:0 
怎么没有高手来解决下!用VB6.0,或者VBA都可以正确获取全部的源码的,唯独就是VS下写的代码就获取不正常。但是在本地获取源代码的话,电脑要随时开着,时间长了电脑受不了,于是用C#写代码,把它放到远程服务器上,来执行代码获取并过滤写入数据库,望求高手解答,谢谢!
2016-02-29 22:10
福宝相安
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-2-28
收藏
得分:0 
现在的问题就是C#写代码获取的源代码就不正常,不过上面的代码有些网站是可以正常获取的,所以说很郁闷!
2016-02-29 22:14
a646404908
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:189
专家分:492
注 册:2012-2-14
收藏
得分:0 
request.AllowAutoRedirect = false;
由于你设置这个属性。。他就不能读取一些做过转跳的网站的源代码.
所以把 request.AllowAutoRedirect =true就ok了。。
2016-04-11 12:03
快速回复:C#无法正确获取网站页面的源码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018942 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved