如何使用HttpWebRequest仿照IE?

ginffilm 2005-10-25 06:10:39
如何使用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…………
...全文
230 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ginffilm 2005-10-26
  • 打赏
  • 举报
回复
http://dl2.www2.btchina.net/download.php?s=69be47b41396b5df&attachmentid=539293
这个问题解决了,
解决的办法是添加了useragent和referer

但是下载页面http://bt2.btchina.net时返回的是乱码,而且返回来的内容也和IE返回来的不一样.我用的是Default编码,好象utf-8也不行,不知道怎么设置才能返回正确的编码和内容,请问高手这个如何解决呀.

在下载 www.macromedia.com 也是返回来其他的页面.
poboy 2005-10-26
  • 打赏
  • 举报
回复
他竟然检测到我没flash
我明明有
Accept
application/x-shockwave-flash
poboy 2005-10-26
  • 打赏
  • 举报
回复
不过虽然我这样设置,
但 在下载 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
帮你顶 ,
帮我顶
poboy 2005-10-26
  • 打赏
  • 举报
回复

我的浏览器的 头信息:

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料
ginffilm 2005-10-26
  • 打赏
  • 举报
回复
怎么没人知道吗?听说这里都是高手的 哦!

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧