0

I'm new to C# form, recently trying to write a program which can download files from my friend's online hard drive. But my program can only download file size lower than 1mb correctly, beyond that can only download 15bytes. I did some research that seems to be maxRecievedMessageSize problem, i tried to check my app.config, and this is what i got

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

And this is my code for download

MyHttpResponse httpDownload(String url)
    {
        string strHtml = string.Empty;
        MyHttpResponse myResponse = new MyHttpResponse();

            WebRequest myWebRequest = WebRequest.Create(url);
            myWebRequest.Timeout = 10000;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
            myHttpWebRequest.Timeout = 10000;
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; Windows NT 6.0; Windows NT 6.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; MS-RTC LM 8; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 4.0C; .NET CLR 4.0E)";

            myHttpWebRequest.CookieContainer = this.cookieContainer;

            WebResponse myWebResponse = myHttpWebRequest.GetResponse();
            Stream myStream = myWebResponse.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myStream, Encoding.Default);

            int index1 = url.LastIndexOf('/');
            int index2 = url.LastIndexOf('?');
            String FileName = url.Substring(index1 + 1, index2 - index1 - 1);

            WebClient myWebClient = new WebClient();
            //myWebClient.DownloadDataCompleted += new AsyncCompletedEventHandler(DownloadProgressCallback);
            //trying to have this function but not sure how to code it right...
            string filepath = textBox3.Text;
            myWebClient.DownloadFileAsync(new Uri(url), filepath + "/" + FileName);
        }

someone said DownloadFileAsync can "absolutely successfully download a file so the issue is my code or my setup" Is there anything else i can provide to give more informations?

I've saw c# - maxReceivedMessageSize and maxBufferSize in app.config, but don't really understand what it's saying.

By the way, not sure if it's about WCF though.

rene
  • 41,474
  • 78
  • 114
  • 152
RLC
  • 31
  • 1
  • 3
  • If I just use the last 3 lines of the code you show I can download files from [here](https://archive.org/download/stackexchange) with any size. See if you can as well. – rene Dec 28 '17 at 16:02
  • We don't need *Thank you* in the question. We assume you're thankful by default. – rene Dec 28 '17 at 16:04
  • I am so sorry, i did something idiot in the code, it works just fine after i fixed it, thanks everyone, and i am very sorry – RLC Jan 01 '18 at 15:05

1 Answers1

0

Look at the contents of the 15k file that you received. It probably contains crucial information. I expect it to be HTML content containing an error message. The problem might be at your friend's side; not on yours.

Some (web) services set a maximum file size to be served.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • but i can download any size by using browsers, is it still the site's problem? or there is something wrong with mine? – RLC Dec 28 '17 at 16:06
  • If you have a file that works with a browser but not with your client then you should probably look at your client. Still, if you say that your downloaded large file becomes 15k only, **look at its contents**. Open it in notepad or rename to HTML and drag it to a browser window. I'd be very interested in what it says if I were you. – Wouter van Nifterick Dec 28 '17 at 16:13
  • thank you very much, i just checked the content, seems like something is wrong with my code – RLC Dec 28 '17 at 16:23
  • I am so sorry, i did something idiot in the code, it works just fine after i fixed it, thanks everyone, and i am very sorry – RLC Jan 01 '18 at 15:05