none
problem with WebClient object RRS feed

  • Question

  • I have a method that use a WebClient object to copy image files from a specified Uri. In the calling method, I have a text file that contains a list of Uri to loop through.  The core part of webclient method looks like this

            public static void CopyFromHttp(string fromFile, string toFile)
            {

                       Uri picUri = new Uri(fromFile);

                        CreateFileDir(toFile);

                        WebClient client = new WebClient();

                        client.DownloadFile(picUri, toFile);
           }
                   
    It works fine. But when I tried to eliminate the Uri that does not contain an image file by using ContenType property of a HttpWebRequest object, the client.DownloadFile method just hang there when processing the second line of the uri list, till a time-out exception is thrown. I have no idea how this happens. The parameters for picUri and Tofile all passed fine for the second line, but the client.DownloadFile method just hang there! Below is the same method with added response content type check:
                   
            public static void CopyFromHttp(string fromFile, string toFile)
            {

                       Uri picUri = new Uri(fromFile);

                     //   Check to see if picUri returns a gif or jpg file

                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUri);
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        if (response.ContentType == "image/jpeg" || response.ContentType == "image/gif")
                        {
                            CreateFileDir(toFile);

                            WebClient client = new WebClient();

                            client.DownloadFile(picUri, toFile);
     
                        }              

                    }


    I am really puzzled at this. Any ideas would be appreciated!
    • Moved by Mariya Atanasova [Microsoft Edge] Wednesday, August 6, 2008 3:12 PM duplicate thread in both forums. Moving to the main one and merging threads (Moved from .NET Base Class Library to .NET Framework Setup)
    Tuesday, August 5, 2008 2:15 AM

Answers

  • In your current code you are making to requests to the web server.  One to check the headers and one to get the file.  You should use the GetResponseStream method of the HttpWebResponse to read the response instead of making the second request.


            public static void CopyFromHttp(string fromFile, string toFile)
            {

                Uri picUri = new Uri(fromFile);

                //   Check to see if picUri returns a gif or jpg file

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUri);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.ContentType == "image/jpeg" || response.ContentType == "image/gif")
                {
                    CreateFileDir(toFile);

                    Stream stream = response.GetResponseStream();

                    using (FileStream fileStream = File.Open(toFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        byte[] buffer = new byte[0x400];
                        int bytesRead = stream.Read(buffer, 0, 0x400);
                        while (bytesRead != 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                            bytesRead = stream.Read(buffer, 0, 0x400);
                        }
                        fileStream.Flush();
                        fileStream.Close();
                    }

                    //WebClient client = new WebClient();
                    //client.DownloadFile(picUri, toFile);
                }

            }

    Tuesday, August 5, 2008 4:09 AM
  • Yes, there can be, but you have to make asynchronous calls. In your case you're making synchronous calls and blocking on the first one.

    Thanks
    Mariya

    Wednesday, August 6, 2008 3:10 PM

All replies

  • In your current code you are making to requests to the web server.  One to check the headers and one to get the file.  You should use the GetResponseStream method of the HttpWebResponse to read the response instead of making the second request.


            public static void CopyFromHttp(string fromFile, string toFile)
            {

                Uri picUri = new Uri(fromFile);

                //   Check to see if picUri returns a gif or jpg file

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUri);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.ContentType == "image/jpeg" || response.ContentType == "image/gif")
                {
                    CreateFileDir(toFile);

                    Stream stream = response.GetResponseStream();

                    using (FileStream fileStream = File.Open(toFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        byte[] buffer = new byte[0x400];
                        int bytesRead = stream.Read(buffer, 0, 0x400);
                        while (bytesRead != 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                            bytesRead = stream.Read(buffer, 0, 0x400);
                        }
                        fileStream.Flush();
                        fileStream.Close();
                    }

                    //WebClient client = new WebClient();
                    //client.DownloadFile(picUri, toFile);
                }

            }

    Tuesday, August 5, 2008 4:09 AM
  • Thanks for the help! I also tried to free connection by adding response.close() after the line
    HttpWebResponse response = (HttpWebResponse)request.GetResponse() . And this time the client.DownloadFile() method executed fine. So the idea is that there can't be two server requests at the same time?
    Wednesday, August 6, 2008 12:23 AM
  • Yes, there can be, but you have to make asynchronous calls. In your case you're making synchronous calls and blocking on the first one.

    Thanks
    Mariya

    Wednesday, August 6, 2008 3:10 PM