1

I used to work with Webclient and WebClient have file download completed event. But now i'm using this method:

private static void FileDownload(string uri, string fileName)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if ((response.StatusCode == HttpStatusCode.OK ||
                response.StatusCode == HttpStatusCode.Moved ||
                response.StatusCode == HttpStatusCode.Redirect) &&
                response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
            {

                using (Stream inputStream = response.GetResponseStream())
                using (Stream outputStream = File.OpenWrite(fileName))
                {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    do
                    {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);
                }
            }
        }

Dows the WebRequest , WebResponse or maybe the Stream have something like a completed event ? So if the file downloaded ok i can do stuff ? Like the WebClient file download completed event.

Edit:

I'm trying to use the async way so i did this so far:

HttpWebRequest request;
        void FileDownload(string uri, string fileName)
        {
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.AllowAutoRedirect = false;
            request.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if ((response.StatusCode == HttpStatusCode.OK ||
                response.StatusCode == HttpStatusCode.Moved ||
                response.StatusCode == HttpStatusCode.Redirect) &&
                response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
            {

                using (Stream inputStream = response.GetResponseStream())
                using (Stream outputStream = File.OpenWrite(fileName))
                {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    do
                    {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);
                }
            }

        }

And then:

        void FinishWebRequest(IAsyncResult result)
        {
            request.EndGetResponse(result);

            if (!result.IsCompleted)
            {
            }
        }

The problem is now that at this part: response.ContentType the ContentType is empty "" so it dosen't enter and download the file.

It did before i changed/added the request.BeginGetResponse(new AsyncCallback(FinishWebRequest), null); and the FinishWebRequest.

  • I think you should look at async calls of HttpWebResponse, for example http://stackoverflow.com/questions/202481/how-to-use-httpwebrequest-net-asynchronously – sarh Jan 18 '15 at 01:01
  • It's a really bad idea to get the response then not consume it. You should, at the very least, call `request.Abort()` and `response.Dispose()` if you decide to skip reading the response. – spender Jan 18 '15 at 01:34
  • I edited my question with the async code i tried. If you can see it and fix it or tell me what i did wrong ? Now the ContentType that should be "image" is empty it's "" –  Jan 18 '15 at 01:55

1 Answers1

0

In the end i did it this way:

HttpWebRequest request;
        void fileDownloadRadar(string uri, string fileName)
        {
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
            request.CookieContainer = new CookieContainer();
            request.AllowAutoRedirect = true;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.ContentType == "")
            {
                Logger.Write("ContentType is Empty download was not fine !!!!!");
            }
            if ((response.StatusCode == HttpStatusCode.OK ||
                response.StatusCode == HttpStatusCode.Moved ||
                response.StatusCode == HttpStatusCode.Redirect) &&
                response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
            {
                Logger.Write("ContentType is not empty meaning download is fine");
                using (Stream inputStream = response.GetResponseStream())
                using (Stream outputStream = File.OpenWrite(fileName))
                {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    do
                    {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);

                }
                FinishWebRequest();
            }
            else
            {
                timer1.Stop();
                timer3.Start();
            }
        }

It's not async like i wanted but it's working perfect like i needed. Maybe i will use a backgroundworker but this is working just like i needed.