.net - C# Code to download an image from a "not so easy" CDN -


im trying download image specific website.
actually, code , running in production months, it's not able download imagens specific website

the image url need download one: (for instance) http://static7.kabum.com.br/produtos/fotos/64297/64297_index_g.jpg

the codes tried far:

method 1 -> (failed)

string url = "http://static7.kabum.com.br/produtos/fotos/64297/64297_index_g.jpg"; var request = (httpwebrequest)webrequest.create(url); request.timeout = (timeout == 0 ? 30 : timeout) * 1000; request.keepalive = false; request.useragent = "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, gecko) chrome/48.0.2564.116 safari/537.36";  var response = (httpwebresponse)request.getresponse(); if (response.statuscode == httpstatuscode.ok) {     const int buffer_size = 16 * 1024;     var buffer = new byte[buffer_size];      // if remote file found, download     using (stream inputstream = response.getresponsestream())     using (stream outputstream = file.create(filename, buffer_size))     {         int bytesread;                 {             bytesread = inputstream.read(buffer, 0, buffer.length);             outputstream.write(buffer, 0, bytesread);         } while (bytesread != 0);     } } 

method 2 -> (also failed)

[..] using(image webimage = image.fromstream(response.getresponsestream())) {     webimage.save(filename); } [..] 

both methods fail following exception

“parameter not valid” exception loading system.drawing.image

stacktrace = " em system.drawing.image.fromstream(stream stream, boolean useembeddedcolormanagement, boolean validateimagedata) in system.drawing.image.fromstream(stream stream) in monitorlib.helper.requestpagehelper.requestdowloadpage(boolean proxy, strin...

i guess image data incomplete or compacted, url works fine on browser

any thoughts? lot friends

you use webclient.downloadfile() method.

var filename = @"c:\path\to\file.jpg"; var url = "http://static7.kabum.com.br/produtos/fotos/64297/64297_index_g.jpg";  using (var client = new webclient()) {     client.downloadfile(url, filename); } 

Comments