如何使用HttpWebRequest仿照IE?
如何使用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…………