2014-05-07 37 views
0

我有以下代码从HTTPServer下载一组文件。它给出了第一对文件的响应,稍后会陷入获得响应。我可以从我创建的日志文件中验证这一点。在日志文件中,它会写入“在webresponse之前”,但从未达到第三个文件的“webresponse”之后。我已经在使用web响应。这里有什么可爱的?HTTPResponse挂起多个请求

  Logger.WriteToLog("url = " +url); 
      // Create a request to the file we are downloading 
      HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
      webRequest.Timeout = 120000; 
      webRequest.ReadWriteTimeout = 300000; 

      // Set default authentication for retrieving the file 
      //webRequest.Credentials = new NetworkCredential(GlobalVariables.username, GlobalVariables.password); 
      webRequest.UseDefaultCredentials = true; 

      // Retrieve the response from the server 
      Logger.WriteToLog("Before webresponse"); 
      using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
      { 
       Logger.WriteToLog("After webresponse"); 

       // Ask the server for the file size and store it 
       //Int64 fileSize = webResponse.ContentLength; 

       // Open the URL for download 
       using (Stream strResponse = webResponse.GetResponseStream()) 
       { 
        // Create a new file stream where we will be saving the data (local drive) 
        strLocal = File.Create(destFilePath); 

        // Loop through the buffer until the buffer is empty 
        while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) 
        { 
         if (isPaused) 
          waitRun_m.WaitOne(); 

         if (isCanceled) 
          break; 

         strLocal.Write(downBuffer, 0, bytesSize); 
         // Invoke the method that updates the form's label and progress bar 
         UpdateProgessCallback(mediaName, bytesSize); 
        }; 

        strResponse.Close(); 
       } 

      } 
+0

destFilePath在ecach响应上应该是不同的,或者你可能有一些文件锁定问题。 –

+0

是的。每个响应的destFilePath都不相同。 –

+0

写入后关闭文件。 strLocal.Close();你正在创建Allways文件,或许会更好地创建文件,只有当你有一个响应(即时只是试图改善代码,答案可能有点不同)。 –

回答

0

我尝试了ServicePoint类的方法,现在工作正常。我所做的是在为webrequest添加readwritetimeout之后,我使用以下语句为连接组指定了名称:

webRequest.ConnectionGroupName = some_name;

和关闭连接时所做的:

webRequest.ServicePoint.CloseConnectionGroup(some_name);