The operation completed using WebClient.DownloadFile and fixed the URL

I upload batch files to the database.

I upload the image URLs to the site that will be used for the products.

The code written by me works fine for the first 25 iterations (always this number for some reason), but then throws me a System.Net.WebException "Operation completed".

if (!File.Exists(localFilename))
{
    using (WebClient Client = new WebClient())
    {
        Client.DownloadFile(remoteFilename, localFilename);
    }
}

I checked the remote url it was requesting and this is the correct url of the image that returns the image.

Also, when I go to it using the debugger, I don't get a timeout error.

HELP !;)

+5
source share
4 answers

, , :

  • , System.Net.ServicePointManager.DefaultConnectionLimit. 50-100 . , , , , .: -)
  • , . , , . # 25, , .
  • HTTP- keepalive . .
  • 25 , - , , , > 25 , IP- ( -), .

, , , ( !) .

, : (Thread.Sleep) HTTP- , . , , . , (, 10 ), . 10- , , .

, , - (, , ) -, . , (, DateTime.Now ), , . , //. , , .

, , - - , , - , . WebClient, -. MSDN .

Fiddler:

  • fiddler .
  • -. Proxy - Fiddler (localhost: 8888).
  • .
+14

, WebClient Response, , , , 25 , " ". , - ..... ( WebClient, Reflector ).

HttpWebRequest & HttpWebResponse, :

HttpWebRequest request;
HttpWebResponse response = null;

try
{

   FileStream fs;
   Stream s;
   byte[] read;
   int count;

   read = new byte[256];

   request = (HttpWebRequest)WebRequest.Create(remoteFilename);
   request.Timeout = 30000;
   request.AllowWriteStreamBuffering = false;

   response = (HttpWebResponse)request.GetResponse();
   s = response.GetResponseStream();  

   fs = new FileStream(localFilename, FileMode.Create);   
   while((count = s.Read(read, 0, read.Length))> 0)
   {
      fs.Write(read, 0, count);
      count = s.Read(read, 0, read.Length);
   }

   fs.Close();
   s.Close();
}
catch (System.Net.WebException)
{
    //....
}finally
{
   //Close Response
   if (response != null)
      response.Close();
}
+4

:

    private static void DownloadFile(Uri remoteUri, string localPath)
    {
        var request = (HttpWebRequest)WebRequest.Create(remoteUri);
        request.Timeout = 30000;
        request.AllowWriteStreamBuffering = false;

        using (var response = (HttpWebResponse)request.GetResponse())
        using (var s = response.GetResponseStream())
        using (var fs = new FileStream(localPath, FileMode.Create))
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0)
            {
                fs.Write(buffer, 0, bytesRead);
                bytesRead = s.Read(buffer, 0, buffer.Length);
            }
        }
    }
+2

, , app.config:

<system.net>
  <connectionManagement>
    <add address="*" maxconnection="100" />
  </connectionManagement>
</system.net>
+2

All Articles