C# Download Image from unknown format

c#, html, http

Solution

If you look at the first few bytes of the data you get back with your code, you can see that it starts with `1F 8B 08`. This indicates that the data is gzip'd data (gzip encoding is a common thing on the web). You can include the `AutomaticDecompression` property to make the .Net code automatically decompress this data and get your valid PNG (the bytes start with `89 50 4E 47`):

var request = (HttpWebRequest)HttpWebRequest.Create(DownloadAddress);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

Problem

I'm working on batch downloader but some URLs are not sending data correctly. For example, this page: http://i.imgbox.com/absMQK6A.png In any internet browser, this page shows an image, but in my program, downloads strange data. I think this URL is fake or protected (I don't know HTML well.) BTW, in IE, I can download that image normally with right click and save as image. so I want to emulate that behavior in my program. How can I do this? Below is part of my program's code. ``` HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(DownloadAddress); if (Proxy != null) { request.Proxy = Proxy; } if (!string.IsNullOrWhiteSpace(UserAgent)) { request.UserAgent = UserAgent; } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream downloadHttpStream = response.GetResponseStream(); int read = downloadHttpStream.Read(buffer, 0, buffer.Length); // output codes ``` UserAgent is string that has informations of browser. such as IE, Firefox etc. Thanks.

Original source

Related problems