WebClient wc=new WebClient();
byte[] be=wc.DownloadData("http://128.adsina.allyes.com/main/adfshow?user=AFP6_for_SINA|home|homebanner01new&db=sina&border=0&local=yes");
Encoding ec=System.Text.Encoding.GetEncoding("gb2312");
string news=ec.GetString(be);正常情况下news字符串应该是wc.DownloadData("url")中URL的源代码,但是上面
的例子取到的不对(跟直接在地址栏里打开网站查看到的源代码不一样),
并且所有的这类型广告地址下载下来的都是一样的源代码。
请教如何处理才能下载下正确的源代码。谢谢

解决方案 »

  1.   

    两个原因,
    1 。encoding ,2 ,http头信息
    public static string  GetHtml(string url)
    {
    HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
    myHttpWebRequest.Accept=@"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";   
    myHttpWebRequest.UserAgent=@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";


    try
    {
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
                   
    Stream streamResponse=myHttpWebResponse.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse,Encoding.Default);
    string htmlcode=streamRead.ReadToEnd();
    streamRead.Close();

    streamResponse.Close();
    myHttpWebResponse.Close();

    return htmlcode; } catch(Exception e)
    {

    return "error";
    }
    }
    };保证好用
      

  2.   

    to : poboy试过了不行的,http头信息我加上也不行。
      

  3.   

    是用的me的代码么 ,如果不是
    那个头信息必须在
    HttpWebResponse之前加的 ,
    绝不能再之后。。
      

  4.   

    to:poboy
    试过了,直接把你的代码拷过去也是不行。
      

  5.   

    没招料 ,小弟整整一个上午 ,
    都没搞定,才疏学浅哪 ,
    大哥解决时发个贴  ,
    写上方法head属性一个一个的设  ,不行哪 只获得 
    一个空的1*1 图片服料。。
      

  6.   

    to :poboy 我也是啊。Head属性都试过的。不过用PHP代码可以