开发者

image saving from a URL problem

I am trying to download an image from URL: http://appworld.blackberry.com/webstore/servedimages/340582.png?t=2

I am using HttpWebRequest webRespose Stream BinaryReader FileStream BinaryWriter

this works for other websites and images but the url I gave on the above gives me an empty 0 byte file..

which means I cannot save 开发者_如何学JAVAthe image from that URL.

Is there anybody who can help me?


I am using HttpWebRequest webRespose Stream BinaryReader FileStream BinaryWriter

Why use 6 different classes when you can do the same with only one?

string sourceUrl = "http://appworld.blackberry.com/webstore/servedimages/340582.png?t=2";
string localPath = @"C:\foo\bar\340582.png";
using (WebClient wc = new WebClient())
{
    wc.DownloadFile(sourceUrl, localPath);
}

If you need to load an image from this URL, you can do that (I assume you're using WinForms/GDI):

string sourceUrl = "http://appworld.blackberry.com/webstore/servedimages/340582.png?t=2";
string localPath = @"C:\foo\bar\340582.png";
Image image;
using (WebClient wc = new WebClient())
using (Stream stream = wc.OpenRead(sourceUrl))
{
    image = Image.FromStream(stream);
}


I wonder if you have hit their site way too many times and they are now blocking you for abuse...

Most likely they are examining the headers sent and not allowing bots to grab their intellectual property.

Two possible solutions come to mind:

  1. Change your request code to pass believable browser headers.
  2. Call blackberry.com and get permission to reuse their files.

You should probably do both.

Point is, I doubt it's a code issue and more likely a violation of their terms of service.


and for fun:

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
// Setting the useragent seems to resolve certain issues that *may* crop up. 
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";


I just tested this out and this downloaded and saved the image for me. You will probably need to check for MIME types and all that somewhere along the road.

string url = "http://appworld.blackberry.com/webstore/servedimages/340582.png?t=2";

using (System.Drawing.Image img = System.Drawing.Image.FromStream(WebRequest.Create(url).GetResponse().GetResponseStream())) {
    img.Save("new.jpg");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜