How to make webclient download file again if failed?









up vote
2
down vote

favorite












I'm trying to download a list of links of images to my server (Up to 40 links) using foreach.



In my case sometimes the link exists but I don't know why it's going to catch and cancel the download of the next link. Maybe it needs to wait for a little? because when I debug the app I see that the link was the application skipped and went to catch was available but sometimes it's open after few seconds in my browser so the response time from the server I trying to download sometimes need more time to load and open the link.



 string newPath = "~/data/" + model.PostID + "/" + name + "/";

//test1 is a list of links
foreach (var item1 in test1)


HttpWebRequest request = WebRequest.Create(item1) as HttpWebRequest; request.Method = "HEAD";
try

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

var webClient = new WebClient();
string path = newPath + i + ".jpg";
webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
string newlinks = "https://example.com/data/" + chapter.PostID + "/" + name + "/" + i + ".jpg";
allimages = allimages + newlinks + ',';
response.Close();
i++;




catch

break;







Now my main goal is to fix this issue but as I saw in debugging:



  1. The Images Links I'm trying to download exists


  2. Sometimes Need More Time to response


So How I can fix this ? when download cancel and a link exists, what I should do?










share|improve this question























  • Keep track of failed downloads, and try them again (i.e. an extra loop).
    – mjwills
    Nov 10 at 11:45










  • Possible duplicate of How to change the timeout on a .NET WebClient object
    – mjwills
    Nov 10 at 11:46














up vote
2
down vote

favorite












I'm trying to download a list of links of images to my server (Up to 40 links) using foreach.



In my case sometimes the link exists but I don't know why it's going to catch and cancel the download of the next link. Maybe it needs to wait for a little? because when I debug the app I see that the link was the application skipped and went to catch was available but sometimes it's open after few seconds in my browser so the response time from the server I trying to download sometimes need more time to load and open the link.



 string newPath = "~/data/" + model.PostID + "/" + name + "/";

//test1 is a list of links
foreach (var item1 in test1)


HttpWebRequest request = WebRequest.Create(item1) as HttpWebRequest; request.Method = "HEAD";
try

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

var webClient = new WebClient();
string path = newPath + i + ".jpg";
webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
string newlinks = "https://example.com/data/" + chapter.PostID + "/" + name + "/" + i + ".jpg";
allimages = allimages + newlinks + ',';
response.Close();
i++;




catch

break;







Now my main goal is to fix this issue but as I saw in debugging:



  1. The Images Links I'm trying to download exists


  2. Sometimes Need More Time to response


So How I can fix this ? when download cancel and a link exists, what I should do?










share|improve this question























  • Keep track of failed downloads, and try them again (i.e. an extra loop).
    – mjwills
    Nov 10 at 11:45










  • Possible duplicate of How to change the timeout on a .NET WebClient object
    – mjwills
    Nov 10 at 11:46












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to download a list of links of images to my server (Up to 40 links) using foreach.



In my case sometimes the link exists but I don't know why it's going to catch and cancel the download of the next link. Maybe it needs to wait for a little? because when I debug the app I see that the link was the application skipped and went to catch was available but sometimes it's open after few seconds in my browser so the response time from the server I trying to download sometimes need more time to load and open the link.



 string newPath = "~/data/" + model.PostID + "/" + name + "/";

//test1 is a list of links
foreach (var item1 in test1)


HttpWebRequest request = WebRequest.Create(item1) as HttpWebRequest; request.Method = "HEAD";
try

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

var webClient = new WebClient();
string path = newPath + i + ".jpg";
webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
string newlinks = "https://example.com/data/" + chapter.PostID + "/" + name + "/" + i + ".jpg";
allimages = allimages + newlinks + ',';
response.Close();
i++;




catch

break;







Now my main goal is to fix this issue but as I saw in debugging:



  1. The Images Links I'm trying to download exists


  2. Sometimes Need More Time to response


So How I can fix this ? when download cancel and a link exists, what I should do?










share|improve this question















I'm trying to download a list of links of images to my server (Up to 40 links) using foreach.



In my case sometimes the link exists but I don't know why it's going to catch and cancel the download of the next link. Maybe it needs to wait for a little? because when I debug the app I see that the link was the application skipped and went to catch was available but sometimes it's open after few seconds in my browser so the response time from the server I trying to download sometimes need more time to load and open the link.



 string newPath = "~/data/" + model.PostID + "/" + name + "/";

//test1 is a list of links
foreach (var item1 in test1)


HttpWebRequest request = WebRequest.Create(item1) as HttpWebRequest; request.Method = "HEAD";
try

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

var webClient = new WebClient();
string path = newPath + i + ".jpg";
webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
string newlinks = "https://example.com/data/" + chapter.PostID + "/" + name + "/" + i + ".jpg";
allimages = allimages + newlinks + ',';
response.Close();
i++;




catch

break;







Now my main goal is to fix this issue but as I saw in debugging:



  1. The Images Links I'm trying to download exists


  2. Sometimes Need More Time to response


So How I can fix this ? when download cancel and a link exists, what I should do?







c# webclient






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 17:36









Gihan Saranga Siriwardhana

2749




2749










asked Nov 10 at 11:42









Dummies EBooks

556




556











  • Keep track of failed downloads, and try them again (i.e. an extra loop).
    – mjwills
    Nov 10 at 11:45










  • Possible duplicate of How to change the timeout on a .NET WebClient object
    – mjwills
    Nov 10 at 11:46
















  • Keep track of failed downloads, and try them again (i.e. an extra loop).
    – mjwills
    Nov 10 at 11:45










  • Possible duplicate of How to change the timeout on a .NET WebClient object
    – mjwills
    Nov 10 at 11:46















Keep track of failed downloads, and try them again (i.e. an extra loop).
– mjwills
Nov 10 at 11:45




Keep track of failed downloads, and try them again (i.e. an extra loop).
– mjwills
Nov 10 at 11:45












Possible duplicate of How to change the timeout on a .NET WebClient object
– mjwills
Nov 10 at 11:46




Possible duplicate of How to change the timeout on a .NET WebClient object
– mjwills
Nov 10 at 11:46












3 Answers
3






active

oldest

votes

















up vote
0
down vote













you can use this example:



 class WebClientUtility : WebClient

public int Timeout get; set;

public WebClientUtility() : this(60000)

public WebClientUtility(int timeout)

this.Timeout = timeout;


protected override WebRequest GetWebRequest(Uri address)

var request = base.GetWebRequest(address);
if (request != null)

request.Timeout = Timeout;

return request;



//
public class DownloadHelper : IDisposable

private WebClientUtility _webClient;
private string _downloadUrl;
private string _savePath;
private int _retryCount;

public DownloadHelper(string downloadUrl, string savePath)

_savePath = savePath;
_downloadUrl = downloadUrl;

_webClient = new WebClientUtility();

_webClient.DownloadFileCompleted += ClientOnDownloadFileCompleted;



public void StartDownload()

_webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);


private void ClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

if (e.Error != null)

_retryCount++;

if (_retryCount < 3)

_webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);

else

Console.WriteLine(e.Error.Message);


else

_retryCount = 0;
Console.WriteLine($"successfully download: # _downloadUrl to # _savePath");


public void Dispose()

_webClient.Dispose();



//
class Program


private static void Main(string args)

for (int i = 0; i < 100; i++)

var downloadUrl = $@"https://example.com/mag-i.pdf";
var savePath = $@"D:DownloadFileFileNamei.pdf";

DownloadHelper downloadHelper = new DownloadHelper(downloadUrl, savePath);

downloadHelper.StartDownload();


Console.ReadLine();




to fix timeout problem you can create a derived class and set the timeout property of the base WebRequest class and
for retry you can use the DownloadFileCompleted event of the WebClient and implement your retry pattern there






share|improve this answer








New contributor




yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote













    You're using the async version of 'DownloadFileAsync'. However you're not awaiting the call, that leaves a mess with unpredicted behaviour.



    Make your method async and then use this:



    await webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));





    share|improve this answer




















    • My Method Already async
      – Dummies EBooks
      Nov 10 at 18:30

















    up vote
    0
    down vote













    This Solved my case:



    await Task.Run(() =>

    webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
    );





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238584%2fhow-to-make-webclient-download-file-again-if-failed%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      you can use this example:



       class WebClientUtility : WebClient

      public int Timeout get; set;

      public WebClientUtility() : this(60000)

      public WebClientUtility(int timeout)

      this.Timeout = timeout;


      protected override WebRequest GetWebRequest(Uri address)

      var request = base.GetWebRequest(address);
      if (request != null)

      request.Timeout = Timeout;

      return request;



      //
      public class DownloadHelper : IDisposable

      private WebClientUtility _webClient;
      private string _downloadUrl;
      private string _savePath;
      private int _retryCount;

      public DownloadHelper(string downloadUrl, string savePath)

      _savePath = savePath;
      _downloadUrl = downloadUrl;

      _webClient = new WebClientUtility();

      _webClient.DownloadFileCompleted += ClientOnDownloadFileCompleted;



      public void StartDownload()

      _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);


      private void ClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

      if (e.Error != null)

      _retryCount++;

      if (_retryCount < 3)

      _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);

      else

      Console.WriteLine(e.Error.Message);


      else

      _retryCount = 0;
      Console.WriteLine($"successfully download: # _downloadUrl to # _savePath");


      public void Dispose()

      _webClient.Dispose();



      //
      class Program


      private static void Main(string args)

      for (int i = 0; i < 100; i++)

      var downloadUrl = $@"https://example.com/mag-i.pdf";
      var savePath = $@"D:DownloadFileFileNamei.pdf";

      DownloadHelper downloadHelper = new DownloadHelper(downloadUrl, savePath);

      downloadHelper.StartDownload();


      Console.ReadLine();




      to fix timeout problem you can create a derived class and set the timeout property of the base WebRequest class and
      for retry you can use the DownloadFileCompleted event of the WebClient and implement your retry pattern there






      share|improve this answer








      New contributor




      yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        up vote
        0
        down vote













        you can use this example:



         class WebClientUtility : WebClient

        public int Timeout get; set;

        public WebClientUtility() : this(60000)

        public WebClientUtility(int timeout)

        this.Timeout = timeout;


        protected override WebRequest GetWebRequest(Uri address)

        var request = base.GetWebRequest(address);
        if (request != null)

        request.Timeout = Timeout;

        return request;



        //
        public class DownloadHelper : IDisposable

        private WebClientUtility _webClient;
        private string _downloadUrl;
        private string _savePath;
        private int _retryCount;

        public DownloadHelper(string downloadUrl, string savePath)

        _savePath = savePath;
        _downloadUrl = downloadUrl;

        _webClient = new WebClientUtility();

        _webClient.DownloadFileCompleted += ClientOnDownloadFileCompleted;



        public void StartDownload()

        _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);


        private void ClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

        if (e.Error != null)

        _retryCount++;

        if (_retryCount < 3)

        _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);

        else

        Console.WriteLine(e.Error.Message);


        else

        _retryCount = 0;
        Console.WriteLine($"successfully download: # _downloadUrl to # _savePath");


        public void Dispose()

        _webClient.Dispose();



        //
        class Program


        private static void Main(string args)

        for (int i = 0; i < 100; i++)

        var downloadUrl = $@"https://example.com/mag-i.pdf";
        var savePath = $@"D:DownloadFileFileNamei.pdf";

        DownloadHelper downloadHelper = new DownloadHelper(downloadUrl, savePath);

        downloadHelper.StartDownload();


        Console.ReadLine();




        to fix timeout problem you can create a derived class and set the timeout property of the base WebRequest class and
        for retry you can use the DownloadFileCompleted event of the WebClient and implement your retry pattern there






        share|improve this answer








        New contributor




        yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



















          up vote
          0
          down vote










          up vote
          0
          down vote









          you can use this example:



           class WebClientUtility : WebClient

          public int Timeout get; set;

          public WebClientUtility() : this(60000)

          public WebClientUtility(int timeout)

          this.Timeout = timeout;


          protected override WebRequest GetWebRequest(Uri address)

          var request = base.GetWebRequest(address);
          if (request != null)

          request.Timeout = Timeout;

          return request;



          //
          public class DownloadHelper : IDisposable

          private WebClientUtility _webClient;
          private string _downloadUrl;
          private string _savePath;
          private int _retryCount;

          public DownloadHelper(string downloadUrl, string savePath)

          _savePath = savePath;
          _downloadUrl = downloadUrl;

          _webClient = new WebClientUtility();

          _webClient.DownloadFileCompleted += ClientOnDownloadFileCompleted;



          public void StartDownload()

          _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);


          private void ClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

          if (e.Error != null)

          _retryCount++;

          if (_retryCount < 3)

          _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);

          else

          Console.WriteLine(e.Error.Message);


          else

          _retryCount = 0;
          Console.WriteLine($"successfully download: # _downloadUrl to # _savePath");


          public void Dispose()

          _webClient.Dispose();



          //
          class Program


          private static void Main(string args)

          for (int i = 0; i < 100; i++)

          var downloadUrl = $@"https://example.com/mag-i.pdf";
          var savePath = $@"D:DownloadFileFileNamei.pdf";

          DownloadHelper downloadHelper = new DownloadHelper(downloadUrl, savePath);

          downloadHelper.StartDownload();


          Console.ReadLine();




          to fix timeout problem you can create a derived class and set the timeout property of the base WebRequest class and
          for retry you can use the DownloadFileCompleted event of the WebClient and implement your retry pattern there






          share|improve this answer








          New contributor




          yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          you can use this example:



           class WebClientUtility : WebClient

          public int Timeout get; set;

          public WebClientUtility() : this(60000)

          public WebClientUtility(int timeout)

          this.Timeout = timeout;


          protected override WebRequest GetWebRequest(Uri address)

          var request = base.GetWebRequest(address);
          if (request != null)

          request.Timeout = Timeout;

          return request;



          //
          public class DownloadHelper : IDisposable

          private WebClientUtility _webClient;
          private string _downloadUrl;
          private string _savePath;
          private int _retryCount;

          public DownloadHelper(string downloadUrl, string savePath)

          _savePath = savePath;
          _downloadUrl = downloadUrl;

          _webClient = new WebClientUtility();

          _webClient.DownloadFileCompleted += ClientOnDownloadFileCompleted;



          public void StartDownload()

          _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);


          private void ClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

          if (e.Error != null)

          _retryCount++;

          if (_retryCount < 3)

          _webClient.DownloadFileAsync(new Uri(_downloadUrl), _savePath);

          else

          Console.WriteLine(e.Error.Message);


          else

          _retryCount = 0;
          Console.WriteLine($"successfully download: # _downloadUrl to # _savePath");


          public void Dispose()

          _webClient.Dispose();



          //
          class Program


          private static void Main(string args)

          for (int i = 0; i < 100; i++)

          var downloadUrl = $@"https://example.com/mag-i.pdf";
          var savePath = $@"D:DownloadFileFileNamei.pdf";

          DownloadHelper downloadHelper = new DownloadHelper(downloadUrl, savePath);

          downloadHelper.StartDownload();


          Console.ReadLine();




          to fix timeout problem you can create a derived class and set the timeout property of the base WebRequest class and
          for retry you can use the DownloadFileCompleted event of the WebClient and implement your retry pattern there







          share|improve this answer








          New contributor




          yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 10 at 14:51









          yousef

          113




          113




          New contributor




          yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          yousef is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






















              up vote
              0
              down vote













              You're using the async version of 'DownloadFileAsync'. However you're not awaiting the call, that leaves a mess with unpredicted behaviour.



              Make your method async and then use this:



              await webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));





              share|improve this answer




















              • My Method Already async
                – Dummies EBooks
                Nov 10 at 18:30














              up vote
              0
              down vote













              You're using the async version of 'DownloadFileAsync'. However you're not awaiting the call, that leaves a mess with unpredicted behaviour.



              Make your method async and then use this:



              await webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));





              share|improve this answer




















              • My Method Already async
                – Dummies EBooks
                Nov 10 at 18:30












              up vote
              0
              down vote










              up vote
              0
              down vote









              You're using the async version of 'DownloadFileAsync'. However you're not awaiting the call, that leaves a mess with unpredicted behaviour.



              Make your method async and then use this:



              await webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));





              share|improve this answer












              You're using the async version of 'DownloadFileAsync'. However you're not awaiting the call, that leaves a mess with unpredicted behaviour.



              Make your method async and then use this:



              await webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 10 at 17:49









              Poul Bak

              4,8173732




              4,8173732











              • My Method Already async
                – Dummies EBooks
                Nov 10 at 18:30
















              • My Method Already async
                – Dummies EBooks
                Nov 10 at 18:30















              My Method Already async
              – Dummies EBooks
              Nov 10 at 18:30




              My Method Already async
              – Dummies EBooks
              Nov 10 at 18:30










              up vote
              0
              down vote













              This Solved my case:



              await Task.Run(() =>

              webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
              );





              share|improve this answer
























                up vote
                0
                down vote













                This Solved my case:



                await Task.Run(() =>

                webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
                );





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This Solved my case:



                  await Task.Run(() =>

                  webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
                  );





                  share|improve this answer












                  This Solved my case:



                  await Task.Run(() =>

                  webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
                  );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 18:32









                  Dummies EBooks

                  556




                  556



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238584%2fhow-to-make-webclient-download-file-again-if-failed%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3