public static string DownloadData(string url, Encoding encoding)
        {
            WebClient web = new WebClient();
            return encoding.GetString(web.DownloadData(url));
        }            private void button1_Click(object sender, EventArgs e)
        {
            string htmlstr = DownloadData("http://china.alibaba.com/member/join/common_join.htm")
        }获取不到这个页面的源码。。用HttpWebRequest也不行  请教大家这种情况要怎么弄?把地址换成http://china.alibaba.com/就可以

解决方案 »

  1.   

    怀疑那个页面做了什么判断,如果不是浏览器直接访问,会直接转到一个无法访问的页面上去。当然如果仅是这个问题,你可以加上对应的head,来完全模仿浏览器。
      

  2.   

    嗯一直提示无法链接远程服务器!加上对应的head 具体代码要怎么写呢 
      

  3.   

    以下zzxap出品  参考爬虫,又称蜘蛛,是从别的网站抓取资源的一种方法,C#.NET使用爬虫的方法如下:
    protected string GetPageHtml(string url)
    {
    string pageinfo;
    try
    {
    WebRequest myreq = WebRequest.Create(url);
    WebResponse myrep = myreq.GetResponse();
    StreamReader reader = new StreamReader(myrep.GetResponseStream(), Encoding.GetEncoding("gb2312"));
    pageinfo = reader.ReadToEnd();
    }
    catch
    {
    pageinfo = "";
    }
    return pageinfo;
    }
    按上述方法就可以在程序中获取某URL的页面源文件。
    但是有些网站屏蔽了爬虫,那就需要模拟浏览器获取的方法来进行,具体代码如下:
    protected string GetPageHtml(string url)
    {
    string pageinfo;
    try
    {
    HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
    myReq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
    myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
    HttpWebResponse myRep = (HttpWebResponse)myReq.GetResponse();
    Stream myStream = myRep.GetResponseStream();
    StreamReader sr = new StreamReader(myStream, Encoding.Default);
    pageinfo = sr.ReadToEnd().ToString();
    }
    catch
    {
    pageinfo = "";
    }
    return pageinfo;
    }
      

  4.   

    多谢awayy1432 但结贴时给不了你分 不知道怎么回事