Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:51:57+00:00 2026-06-11T20:51:57+00:00

I am trying to download files through webclient and the code is like the

  • 0

I am trying to download files through webclient and the code is like the one given below. The problem is that if I get continuous 404 response alot of times, my server reaches to 100% and viewing the event log tells that stack overflow happened. Here the “count” variable is to avoid 0 byte files and count404 is for 404 responses.

    int count = 0; int count404 = 0;
    public Stream DownloadFileThroughWebClient(string strFilePath)
    {
        try
        {
            if (count >= 120 || count404 >= 30)
            {
                count = 0;
                count404 = 0;
                return null;
            }
            System.Threading.Thread.Sleep(1000);
            System.Net.WebClient wc = new System.Net.WebClient();
            var v = wc.DownloadData(strFilePath);
            Stream FileToSave = new MemoryStream(v);
            byte[] bytes = new byte[FileToSave.Length];
            int numBytesToRead = (int)FileToSave.Length;
            if (numBytesToRead > 0)
            {
                count = 0;
                count404 = 0;
                return FileToSave;
            }
            else
            {
                count++;
                count404 = 0;
                return DownloadFileThroughWebClient(strFilePath);
            }
        }
        catch (Exception ex)
        {
            count++;
            count404++;
            return DownloadFileThroughWebClient(strFilePath);
        }
    }

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:51:59+00:00Added an answer on June 11, 2026 at 8:51 pm

    Try this (without using recursion and counts the 404s correctly):

    public Stream DownloadFileThroughWebClient(string strFilePath)
    {
        int count = 0; 
        int count404 = 0;
    
        while (count < 120 && count404 < 30)
        {
            try
            {
                byte[] v;
                using (var wc = new WebClient())
                {
                    v = wc.DownloadData(strFilePath);
                }
    
                if (v.Length > 0)
                {
                    return new MemoryStream(v);
                }
    
                count++;
                count404 = 0;
            }
            catch (WebException ex)
            {
                count++;
                var httpWebResponse = ex.Response as HttpWebResponse;
                if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                {
                    count404++;
                    // you may wanna break out of the loop here since there's no point in continuing 
                }
            }
            catch (Exception ex)
            {
    
            }
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a script that will download files from an FTP server.
I am trying to make a script that will let me download files outside
On a server hosted on google app engine, I am trying to download files
I'm trying to download multiple files using IntentService. The IntentService donwloads them okey as
I have another question :(. I'm trying to download multiple files for my application.
i'm trying to download .zip file from bluehost server to my computer. Like this
Hello I am trying to make a C# program that downloads files but I
I'm trying to download SWF files from a site the traditional way, but a
I'm trying to download a small zip file (1159 bytes) and pipe it through
I'm trying to download a file through my server to me, by streaming the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.