How to download the image of the URL via a proxy using C #?

advertisements

I'm trying to download PNG images to file but I'm doing it through my company network proxy. I found this bit of code to download PNG images. Here's the code:

using (WebClient webClient = new WebClient())
{
    byte [] data = webClient.DownloadData("https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/10555140_10201501435212873_1318258071_n.jpg?oh=97ebc03895b7acee9aebbde7d6b002bf&oe=53C9ABB0&__gda__=1405685729_110e04e71d9");

   using (MemoryStream mem = new MemoryStream(data))
   {
       using (var yourImage = Image.FromStream(mem))
       {
          // If you want it as Png
           yourImage.Save("path_to_your_file.png", ImageFormat.Png) ;
       }
   } 

}

I'm using this bit of code to get passed the proxy, but I'm not sure how to incorporate the code mentioned above to download the images:

HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.Proxy = new WebProxy(PROXY_URL, true);
clientHandler.Proxy.Credentials = new NetworkCredential(PROXY_UID, PROXY_PWD, PROXY_DMN);
var client = new HttpClient(clientHandler);
var result = client.GetStreamAsync(url).Result;

I think I'm close. What do I need to do to make this work?


Try :

HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.Proxy = new WebProxy(PROXY_URL, true);
clientHandler.Proxy.Credentials = new NetworkCredential(PROXY_UID, PROXY_PWD, PROXY_DMN);
var client = new HttpClient(clientHandler);
var stream = client.GetStreamAsync(url).Result;
using (var yourImage = Image.FromStream(stream))
{
  // If you want it as Png
   yourImage.Save("path_to_your_file.png", ImageFormat.Png) ;
}