【发布时间】:2012-02-29 17:22:34
【问题描述】:
我需要下载一些图片(验证码)。 This 就是一个例子。
如果您在任何浏览器中打开该链接,它会显示一个图像,但是当我尝试以代码下载该图像时,它会给我一个 html 页面,其中仅包含指向主页的链接 - http://www.networksolutions.com/。我究竟做错了什么?下面是两个代码示例:
byte[] data = new WebClient().DownloadData(imgURL);
AND
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(imgURL);
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Language", "en-us,en;q=0.5");
req.Headers.Add("Accept-Encoding", "gzip,deflate");
req.KeepAlive = true;
byte[] data;
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (Stream s = response.GetResponseStream())
{
int contentLength = (int)response.ContentLength;
data = new byte[contentLength];
for (int pos = 0; pos < contentLength; ++pos)
{
int len = s.Read(data, pos, contentLength - pos);
pos += len;
}
}
}
【问题讨论】:
-
什么是
imgURL?您提供的链接似乎指向将生成图像的页面。不是图像本身。 -
不,那个网址是实际的图片网址。
标签: c# image http httpwebrequest webclient