Reputation: 902
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?
Upvotes: 1
Views: 2217
Reputation: 292425
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);
}
Upvotes: 6
Reputation: 839
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");
}
Upvotes: 1
Reputation: 88064
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:
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)";
Upvotes: 1