string OpenUrlSource(string url)
        {
            if (url == null || url.Trim() == "")
                return null;
            WebClient wc = new WebClient();//定义
            wc.Credentials = CredentialCache.DefaultCredentials;
            wc.OpenRead(url);
            Byte[] pageData = wc.DownloadData(url);
            return Encoding.UTF8.GetString(pageData);//.ASCII.GetString        }
上面的代码我想查看指定网址的源文件,但是查看的结果是:<script type=\"text/javascript\"> top.location.href = \"http://app.baidu.com/widget?appid=104160\";</script>。而不是网页右键->查看源文件,显示的信息。求高手帮忙解答,有没有其他读取网页html文件的代码?上面的方法我实在不知道有什么问题。

解决方案 »

  1.   

            public static string aa(string Url)
            {                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                    System.Net.WebResponse wResp = wReq.GetResponse();
                    System.IO.Stream respStream = wResp.GetResponseStream();
                    System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
                    string UrlName = reader.ReadToEnd();
                    return UrlName;
            }调用 string s=  aa("http://www.baidu.com");是不是这样的
      

  2.   

    代码没有问题,是因为百度的页面用js跳转到http://app.baidu.com/widget?appid=104160,正常情况下浏览器会识别并跳转,而WebClient不会执行js。
      

  3.   

    http://topic.csdn.net/u/20090518/21/f31a5be8-59c7-4fac-b625-d787bd438cb6.html
      

  4.   

    很明显是那个网站的代码问题,不是你的代码问题。那JS的意思是那个框架链接到http://app.baidu.com/widget?appid=104160 这个页面,要是这个页面就是你想要的,那么OpenUrlSource(string url)的参数就是这个页面地址了。