如何使用HttpWebRequest才能完全仿照IE,从而没有那么多的错误和禁止访问?
例子代码如下(以下载btchina上面的文件为例):
如果没有request.UserAgent = "Mozilla/4.0"; 则提示403禁止访问,现在返回来的值是文件不存在(站点web自定义),如果用IE的话就完全没有问题可以下载.谁能把能下载的代码贴出来呀.谢谢各位大哥了!
private void button2_Click(object sender, System.EventArgs e) 

    Uri uri = new Uri("http://dl2.www2.btchina.net/download.php?s=69be47b41396b5df&attachmentid=539293"); 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
    request.UserAgent = "Mozilla/4.0"; 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 
    request.AllowAutoRedirect = true; 
    byte[] str = System.Text.Encoding.ASCII.GetBytes("username=admin"); 
    request.ContentLength = str.Length; 
    Stream temps = request.GetRequestStream(); 
    temps.Write(str,0,str.Length); 
    HttpWebResponse resp…………

解决方案 »

  1.   


    我的浏览器的 头信息:GET / HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)
    Host: localhost:8080
    Connection: Keep-Alive你放一下
    这样:
    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)";就 ok料
      

  2.   

    不过虽然我这样设置,
    但 在下载 www.macromedia.com
    公司首页时 ,它还是显示 
     <!-- $rcsfile: flashdetection2k.pm,v $ $revision: 1.71 $ : server can't tell if your browser has flash; we don't know how to tell if you have flash, sorry. go to /help on our web site if you know you have flash. meanwhile here's the non-flash version. -->
    和 ie的有所不同 ,没有 显示flash的js代码
    我都哭料 
    5555
    帮你顶  ,
    帮我顶
      

  3.   

    他竟然检测到我没flash
    我明明有
    Accept
    application/x-shockwave-flash
      

  4.   

    http://dl2.www2.btchina.net/download.php?s=69be47b41396b5df&attachmentid=539293
    这个问题解决了,
    解决的办法是添加了useragent和referer但是下载页面http://bt2.btchina.net时返回的是乱码,而且返回来的内容也和IE返回来的不一样.我用的是Default编码,好象utf-8也不行,不知道怎么设置才能返回正确的编码和内容,请问高手这个如何解决呀.在下载 www.macromedia.com 也是返回来其他的页面.