日期:2014-05-19  浏览次数:20416 次

HttpWebRequest访问返回404错误
http://www.ce.cn/这个网站的上的一些图片使用WebClient无法下载,返回404错误,我用HttpWebRequest尝试下载也是返回同样的错误,应该如何设置HttpWebRequest呢。

HttpWebRequest   httpWebRequest   =   (HttpWebRequest)WebRequest.Create(address);
httpWebRequest.UserAgent   =   "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   .NET   CLR   1.0.3705;) ";
httpWebRequest.Method   =   "GET ";
httpWebRequest.ContentType   =   "application/x-www-form-urlencoded ";
HttpWebResponse   httpWebResponse   =   (HttpWebResponse)httpWebRequest.GetResponse();
Stream   stream   =   httpWebResponse.GetResponseStream();
Byte[]   buffer   =   new   Byte[stream.Length];
stream.Read(buffer,   0,   (Int32)stream.Length);

------解决方案--------------------
你下载的图片地址是啥。我刚从ce.cn上面测试了一下可以啊。我的代码很简单。。。
public static void DownFileTest()
{
WebClient DownClient = new WebClient();
DownClient.DownloadFile( "http://www.ce.cn/xwzx/gjss/gdxw/200703/19/W020070319402392057317.jpg ",@ "D:\test.jpg ");
}
------解决方案--------------------
我用这张图片
http://www.ce.cn/frontpage/zttjq/200702/25/W020070302541113977871.gif
这句话报错
Byte[] buffer = new Byte[stream.Length];

我改成这个可以
System.Drawing.Image img = System.Drawing.Image.FromStream(httpWebResponse.GetResponseStream());
pictureBox1.Image = img;

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html