如何使用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…………
例子代码如下(以下载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…………
解决方案 »
- 获取软件安装的路径、复制文件夹、文件调用问题!
- 字符串替换的问题,REPLACE只能替换一种字符
- 关于MessageBox返回值的问题
- 初级菜鸟,问个大家都会我不会的问题?
- 我现在想做一个投票系统,可如何才能实现禁止重复投票呢
- 有懂C 、或VB 的 C#高手请帮忙!!!---------在线等待!
- 自定义控件引用后其中一些属性不显示,但是在其他电脑全部正常
- 简单数学问题,求解[多多散分]
- C#版块精华贴推荐大行动,并推荐给斑竹以丰富精华贴!!!
- XmlDocument.GetElementById()函数的疑惑
- DATAGRID在线编辑时,如何为模板列添加事件?
- tcp,udp通讯的时候如果双方都在LAN中 如何实现通讯呢?
我的浏览器的 头信息: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料
但 在下载 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
帮你顶 ,
帮我顶
我明明有
Accept
application/x-shockwave-flash
的
这个问题解决了,
解决的办法是添加了useragent和referer但是下载页面http://bt2.btchina.net时返回的是乱码,而且返回来的内容也和IE返回来的不一样.我用的是Default编码,好象utf-8也不行,不知道怎么设置才能返回正确的编码和内容,请问高手这个如何解决呀.在下载 www.macromedia.com 也是返回来其他的页面.