简体   繁体   中英

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 the 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM