Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
The Problem:
I have access to a website where I must log on and the site uses forms authentication.Once I am authenticated, I can download a song I want. But, before I can download a song, I must first fill out this survey. I it only a few questions but it is very irritating, mostly because the songs I am downloading, I have not yet heard. So I started to write an app. The problem is the music I download. I wrote the program and it works, but the end results sux. Every music file has some type of audible artifact in it.
Please listen to the beginning of these two songs to get an idea here and here.

Ok here is the code. Redactions are only company names and ips. I believe it has something to do with encoding. Please Help. Now for the code:
C#
try
{
    webRequest = (HttpWebRequest)HttpWebRequest.Create(downloadWebsiteString + currentFileInt.ToString());
    webRequest.CookieContainer = cookieContainer;
    webRequest.Credentials = CredentialCache.DefaultCredentials;
    webResponse = (HttpWebResponse)webRequest.GetResponse();
    fileSizeInt = (int)webResponse.ContentLength;
    this.Invoke(new UpdateOverallProgressCallback(this.UpdateOverallProgress), new object[] { currentFileInt });
    if (webResponse.Headers["Content-Disposition"].Contains(@"filename=""""") == true)
    {
        webResponse.Close();
        webRequest = null;
        this.Invoke(new UpdateFileFinishCallback(this.UpdateFileFinish), new object[] { currentFileInt, false });
    }
    else
    {
        contentDisposition = new ContentDisposition(webResponse.Headers["Content-Disposition"]);
        receiveStream = webResponse.GetResponseStream();
        inputStreamReader = new StreamReader(receiveStream, System.Text.Encoding.Default);
        bufferChar = new Char[2048];
        bytesReadInt = inputStreamReader.Read(bufferChar, 0, bufferChar.Length);
        bytesReadRunningInt = bytesReadInt;
        fileStream = new FileStream(folderString + "\\" + currentFileInt.ToString() + "_" + contentDisposition.FileName, FileMode.Create);
        binaryWriter = new BinaryWriter(fileStream, System.Text.Encoding.Default);
        while (bytesReadInt > 0)
        {
            binaryWriter.Write(bufferChar);
            this.Invoke(new UpdateFileProgressCallback(this.UpdateFileProgress), new object[] { bytesReadRunningInt, fileSizeInt });
            bytesReadInt = inputStreamReader.Read(bufferChar, 0, bufferChar.Length);
            bytesReadRunningInt += bytesReadInt;
        }
    }
}

The problem has to be with the buffer size or the character encoding. Like I said, all other features work well enough. Any thoughts.
P.S. Don't bash me about my style of this or I should have used that. I got it to work using this (minus the artifacts), just help me with those please.
Thanks to all.
Posted
Updated 27-Sep-10 5:49am
v6
Comments
DavidKiryazi 27-Sep-10 5:40am    
it would be much easier to help if you only posted the part you think is causing the problem. For example, I know that the constructor for the form is going to call InitializeComponents() and I doubt the problem is in there....
noxia 27-Sep-10 11:52am    
I removed all the "other" code. However, it should be noted that this download routine is part of a separate thread using System.Threading. Also, when I use a lower buffer size of say 256, the audio artifacts "appear" to disappear, but the dl take much longer. With a higher buffer size, the dl is much faster, but with the unwanted ghosts.
Gary

1 solution

Have a look at this link:

How to do Synchronous and Asynchronous web downloads[^]

Good luck!
 
Share this answer
 
Comments
noxia 27-Sep-10 14:23pm    
You must be unclear in my question or just looking for a good post to make. Either way, your "answer" has nothing to do with my question and is therefore incorrect. I'll restate my problem. I know how to download files. I have done everything correctly according to the link you recommended (which, by the way, is written in C++ and not c#). My problem is that after the file has downloaded completely, there are artifacts in the file.
E.F. Nijboer 27-Sep-10 16:18pm    
Well, I'm sorry for the c++ link instead of the c# link. Must have copied the wrong link. Maybe this is somewhat more helpful.
http://www.yoda.arachsys.com/csharp/readbinary.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900