2011-06-10 9 views
16

पर एक अनपेक्षित त्रुटि आई, क्योंकि मैं यहां हूं क्योंकि मुझे ftp प्रोटोकॉल के माध्यम से कुछ फ़ाइलों को डाउनलोड करते समय कोई समस्या है। यह अजीब है क्योंकि यह कभी-कभी और एक ही फ़ाइल के लिए भी होता है।अंतर्निहित कनेक्शन बंद था:

बस एक परिशुद्धता: मैं (500 मो से 30Go करने के लिए) के लिए बहुत बड़ी फ़ाइलों को डाउनलोड कर रहा हूँ

यहाँ मेरी समारोह से वापस लौटे अपवाद की तरह हैं: (खेद उस में फ्रेंच है)

System.Net.WebException: La connexion sous-jacente a été fermée : Une erreur inattendue s'est produite lors de la réception. à System.Net.FtpWebRequest.CheckError() à System.Net.FtpWebRequest.SyncRequestCallback(Object obj) à System.IO.Stream.Close() à System.Net.ConnectionPool.Destroy(PooledStream pooledStream) à System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) à System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) à System.Net.FtpWebRequest.SyncRequestCallback(Object obj) à System.Net.CommandStream.Abort(Exception e) à System.Net.CommandStream.CheckContinuePipeline() à System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState) à System.Net.FtpDataStream.Dispose(Boolean disposing) à System.IO.Stream.Close() à UtilityClasses.FTP.Download(String srcDirectoryPath, String file, String destDirectoryPath)

डाउनलोड तरीके::

public Dictionary<string, object> Download(string srcDirectoryPath, string file, string destDirectoryPath, int attemptLimitNb, int delay) 
    { 
     int attemptNb = 0; 
     bool downloadFailed; 
     Dictionary<string, object> result = new Dictionary<string,object>(); 

     do 
     { 
      attemptNb++; 
      result = Download(srcDirectoryPath, file, destDirectoryPath); 
      downloadFailed = result["downloadfailed"] != null; 
      if (downloadFailed) Thread.Sleep((int)(1000 * delay)); 
     } 
     while (downloadFailed && attemptNb < attemptLimitNb); 
     return result; 
    } 

public Dictionary<string, object> Download(string srcDirectoryPath, string file, string destDirectoryPath) 
    { 
     Exception downloadFailed = null; 
     Dictionary<string, object> result = new Dictionary<string, object>(); 
     bool fileFound = false; 

     try 
     { 
      if (destDirectoryPath == null || !Directory.Exists(destDirectoryPath)) throw new Exception("Download destination path does not exist"); 
      if (file != null && file != "") 
      { 
       if (file.Contains("/")) 
       { 
        throw new Exception("Invalid file name. Impossible to download"); 
       } 

       Uri serverUri; 
       if (srcDirectoryPath == null || srcDirectoryPath == "") 
       { 
        serverUri = new Uri("ftp://" + this.Server + "/" + file); 
       } 
       else if (Regex.IsMatch(srcDirectoryPath, "^/.*$") || Regex.IsMatch(srcDirectoryPath, "^.*/$")) 
       { 
        throw new Exception("Path must not start and end with '/'"); 
       } 
       else 
       { 
        serverUri = new Uri("ftp://" + this.Server + "/" + srcDirectoryPath + "/" + file); 
       } 

       if (serverUri.Scheme != Uri.UriSchemeFtp) throw new Exception("server URI Scheme does not match FTP URI Scheme"); 

       if (Exists(srcDirectoryPath, file)) 
       { 
        fileFound = true; 

        FtpWebRequest downloadRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri); 
        downloadRequest.Credentials = new NetworkCredential(UserName, Password); 
        downloadRequest.KeepAlive = false; 
        downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
        FtpWebResponse response = (FtpWebResponse)downloadRequest.GetResponse(); 

        Stream responseStream = response.GetResponseStream(); 
        FileStream fileStream = new FileStream(Path.Combine(destDirectoryPath, file), FileMode.Create); 
        byte[] buffer = new byte[2000]; 
        int read = 0; 
        try 
        { 
         do 
         { 
          read = responseStream.Read(buffer, 0, buffer.Length); 
          fileStream.Write(buffer, 0, read); 
          fileStream.Flush(); 
         } 
         while (read != 0); 
        } 
        catch (Exception e) 
        { 
         fileStream.Close(); 
         responseStream.Close(); 
         response.Close(); 
         throw e; 
        } 
        fileStream.Close(); 
        responseStream.Close(); 
        response.Close(); 
       } 
      } 
     } 
     catch (WebException webExcptn) 
     { 
      downloadFailed = webExcptn; 
     } 
     finally 
     { 
      result.Add("filefound", fileFound); 
      result.Add("downloadfailed", downloadFailed); 
     } 

     return result; 
    } 

यहाँ डाउनलोड करने के लिए इस्तेमाल किया कोड है

विधि मौजूद है:

public bool Exists(string srcPath, string elementName) 
    { 
     if (elementName == null || elementName == "") 
     { 
      return false; 
     } 

     Uri serverUri; 
     bool res = false; 

     if (srcPath == null || srcPath == "") 
     { 
      serverUri = new Uri("ftp://" + this.Server); 
     } 
     else if (Regex.IsMatch(srcPath, "^/.*$") || Regex.IsMatch(srcPath, "^.*/$")) 
     { 
      throw new Exception("Path must not start and end with '/'"); 
     } 
     else 
     { 
      serverUri = new Uri("ftp://" + this.Server + "/" + srcPath); 

     } 
     if (serverUri.Scheme != Uri.UriSchemeFtp) throw new Exception("server URI Scheme does not match FTP URI Scheme"); 

     FtpWebRequest listingRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri); 
     listingRequest.Credentials = new NetworkCredential(UserName, Password); 
     listingRequest.KeepAlive = false; 
     listingRequest.Method = WebRequestMethods.Ftp.ListDirectory; 
     FtpWebResponse response = (FtpWebResponse)listingRequest.GetResponse(); 

     Stream responseStream = response.GetResponseStream(); 
     StreamReader streamReader = new StreamReader(responseStream); 
     string ftpElementName; 
     do 
     { 
      ftpElementName = Path.GetFileName(streamReader.ReadLine()); 
      if (ftpElementName == null) break; 
      else 
      { 
       string pattern = "^" + elementName.Replace("[", "\\[").Replace("]", "\\]").Replace("+", "[+]").Replace(".", "[.]") + "$"; 
       if (Regex.IsMatch(ftpElementName, pattern, RegexOptions.IgnoreCase)) 
       { 
        res = true; 
       } 
      } 
     } 
     while (ftpElementName != null && !res); 
     streamReader.Close(); 
     responseStream.Close(); 
     response.Close(); 

     return res; 
    } 

शायद यह एक टाइमआउट समस्या है, लेकिन मैं सच में नहीं पता है। मैंने एक उत्तर के लिए लंबे समय तक खोज की लेकिन सफलता के बिना। शायद आप में से कुछ का समाधान होगा।

///

संपादित करें: कुछ प्रगति:

मैं वी.एस. साथ डिबग मोड में मेरी कोड का परीक्षण किया है और वास्तव में अपवाद के ऊपर एक पिछले एक का परिणाम थी। (मैं जानता हूँ कि नहीं किया जा सका है कि क्योंकि मैं केवल लिखा पिछले अपवाद एक लॉग फ़ाइल में लौट आए)

यहाँ मूल अपवाद है:

Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 

दूसरा अपवाद डाउनलोड विधि कोड के इस हिस्से के कारण होता है :

catch (Exception e) 
{ 
    fileStream.Close(); 
    responseStream.Close(); // <<<<<<<<<<<<<< 
    response.Close(); 
    throw e; 
} 

मैं अपने जांच पर रखने लेकिन ऐसा लगता है कि "समय समाप्ति PB" परिकल्पना सबसे सुसंगत है। मैं आज रात एक बड़े टाइमआउट मूल्य के साथ प्रयास करेंगे।

+1

माफी माँगता हूँ नहीं है:

यह धागा जानकारी का एक बहुत कुछ है। मुझे याद रखने वाला एकमात्र फ्रांसीसी "जे ने पार्ल फ्रांसिस" और "जे वौड्राइस एने किलोो डु पोमे डु टेरे, एस'ल वास प्लैस" है, जिनमें से कोई भी बहुत उपयोगी नहीं है, और शायद बहुत ही वर्तनी है :-) आपने बेहतर किया है नौकरी की तुलना में मैं एक समान स्थिति में कर सकता था। – paxdiablo

+13

आपकी अंग्रेजी वास्तव में बहुत अच्छी है। आप एक ऐसे प्रश्न पूछने में कामयाब रहे हैं जो इस साइट पर आने वाले बहुत से लोगों की तुलना में बेहतर शब्द और बेहतर संरचित है और अपनी पहली भाषा के रूप में अंग्रेजी बोलता है। – mdm

+0

इसके अलावा, यदि आप साइट पर नियमित एफ़टीपी क्लाइंट से कनेक्ट होते हैं तो क्या होता है? क्या आप फाइल डाउनलोड कर सकते हैं? ऐसा लगता है जैसे आपके और सर्वर के बीच कनेक्टिविटी त्रुटि है - आपका कोड ठीक दिखता है। यदि आप अपने कोड को एक अलग एफ़टीपी साइट पर इंगित करते हैं (शायद आपकी मशीन पर एक स्थानीय एफ़टीपी सर्वर)? क्या यह असफल हो जाता है? – mdm

उत्तर

2

यहाँ सब का एक अच्छा धागा कोशिश करने के लिए है:

http://social.msdn.microsoft.com/Forums/en/ncl/thread/47634ec2-4d40-4d3f-b075-8cc92bfa2b24

टाइमआउट बढ़ाने से शायद कम से कम में एक अच्छा विचार है।

+0

मैं इस समाधान को आजमाने जा रहा हूं। – Hariboox

+0

मुझे नहीं पता कि किसी विशेष समस्या को हल किया गया है, लेकिन ऐसा लगता है कि आप क्या अनुभव कर रहे हैं। यह बड़ी फाइलों के साथ जटिल हो सकता है। – ScottE

+0

मुझे लगता है कि मैं अब समाधान से निकट हूं। मेरे नए संपादन को देखने दें – Hariboox

12

बस स्कॉट के निदान को मजबूत करना चाहते हैं, और अधिक विशिष्ट हो। टाइमआउट सबसे अधिक संभावना है।

या तो FtpWebRequest का .NET कार्यान्वयन गलत है या the MSDN document में एक टाइपो है, FtpWebRequest का डिफ़ॉल्ट मान। टाइमआउट -1 (अनंत) नहीं है। यह 100000 (100 सेकंड) है।

इसके अलावा एक और टाइमआउट मुद्दा भी है। कुछ परीक्षणों से पता चला है कि प्रतिक्रियास्ट्रीम में हमेशा 300000 (300 सेकंड) का टाइमआउट मान होता है। मुझे नहीं पता कि यह मान कैसे असाइन किया गया है। वैसे भी, इस मूल्य को बड़ी फ़ाइलों को समायोजित करने के लिए संशोधित करने की आवश्यकता है।

संक्षेप में, समाधान FtpWebRequest.Timeout और Stream.Timeout को पर्याप्त रूप से बड़े मान पर सेट करना है।

+0

मैं पुष्टि कर सकता हूं कि FtpWebRequest.Timeout दस्तावेज़ गलत है। @ हांग सही है ... यह 100 सेकंड है। –

0

यह आपके विंडोज फ़ायरवॉल की सेटिंग्स के साथ किसी समस्या का लक्षण हो सकता है। "सेवाओं" इंटरफ़ेस में "एप्लिकेशन लेयर गेटवे सेवा" को अक्षम करने से यह मेरे लिए तय हो गया।

http://forum.parallels.com/pda/index.php/t-57966.html

+1

लिंक मर चुका है। –