WinForms应用程序中的C#中的FTP超时

时间:2016-11-23 05:38:37

标签: c# winforms ftp

我有一个FTP服务器,我从中提取并解析数据并下载图像。我从未遇到过拖动文本文件(我的数据)的问题,但有两个目录可以从中提取图像,一个使用与下面相同的代码(但显然使用不同的Uri和图像名称数组)。为了获得图像列表,我有一个函数,它拉动另一个文件并返回图像名称的数组,然后我将这个图像名称数组顺序放在循环中基URI的末尾,以下载所有图像。

底部的代码是我用图像名称拉动文本文件的功能。它有自己的URI,来自目录结构的不同部分。有趣的是,如果我在一个已知位置手动创建一个图像数组并提供我的图像拉码,它可以正常工作,但如果我运行这个数组构建FTP函数,我的图像会在GetResponse()行中拉出超时。我已经缩短了超时时间,所以我不必等待很长时间才能失败,但它不会太短,因为我说它可以正常使用硬编码的图像URI列表。我在这里搜索并尝试了几件事情,认为我没有正确释放资源,但我无法弄清楚发生了什么。

抱歉,我不是这方面的专家,但我在这个应用程序中有很多相似的代码,这是我第一次遇到FTP问题。

一个注意事项,我有一个早期版本的这个应用程序工作正常,但我必须承认我在源代码版本控制中非常松懈,并且没有旧代码(doh!)。我改变了现在杀了它的东西。

            //Open a web request based on the URI just built from user selections
            FtpWebRequest imageRequest = (FtpWebRequest)WebRequest.Create(imageUri);
            imageRequest.UsePassive = false;
            imageRequest.UseBinary = true;
            imageRequest.KeepAlive = false;
            imageRequest.Timeout = 2000;
            //Act on that webrequest based on what the user wants to do (i.e. list a directory, list file, download, etc)
            imageRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            try
            {
                //Now get the response from the tool as a stream.
                FtpWebResponse imageResponse = (FtpWebResponse)imageRequest.GetResponse();
                //Thread.Sleep(1000);
                Stream imageResponseStream = imageResponse.GetResponseStream();

                byte[] buffer = new byte[2048];

                FileStream fs = new FileStream(newPath + @"\" + templateImageArray2[x].Split(' ')[0], FileMode.Create);
                int ReadCount = imageResponseStream.Read(buffer, 0, buffer.Length);
                while (ReadCount > 0)
                {
                    fs.Write(buffer, 0, ReadCount);
                    ReadCount = imageResponseStream.Read(buffer, 0, buffer.Length);
                }
                fs.Close();
                imageResponseStream.Close();
            }
            catch (WebException r)
            {
                if (r.Status == WebExceptionStatus.Timeout)
                {
                    //Obtain more detail on error:
                    var response = (FtpWebResponse)r.Response;
                    FtpStatusCode errorCode = response.StatusCode;
                    string errorMessage = response.StatusDescription;
                    MessageBox.Show(errorMessage);
                    //goto RETRY;
                }
                MessageBox.Show(r.Message);
                break;
            }




      string[] tempIDPText = new string[0];
      Uri fileUriLocal = new Uri("ftp://srvrname:password@" + tempIpAddress + "/%2fROOT/DEVICE/HD/" + comboClass.Text + "/data/" + dirName1 + "/" + dirName2 + ".img");
      //Open a web request based on the URI just built from user selections
      FtpWebRequest requestLocal = (FtpWebRequest)WebRequest.Create(fileUriLocal);
      requestLocal.UsePassive = false;
      requestLocal.UseBinary = true;
      requestLocal.KeepAlive = false;
      //Act on that webrequest based on what the user wants to do (i.e. list a directory, list file, download, etc)
      requestLocal.Method = WebRequestMethods.Ftp.DownloadFile;
      try
      {
          //Now get the response from the tool as a stream.
          FtpWebResponse responseLocal = (FtpWebResponse)requestLocal.GetResponse();
          Stream responseStream = responseLocal.GetResponseStream();
          StreamReader reader = new StreamReader(responseStream);

          //Now read an individual file and fill the array with the chamber status
          int y = 0;
          while (!reader.EndOfStream)
          {
              if (reader.EndOfStream)
                  break;
              else
              {
                  Array.Resize(ref tempIDPText, tempIDPText.Length + 1);
                  tempIDPText[tempIDPText.Length - 1] = reader.ReadLine();
              }
          }

          reader.Close();
          responseStream.Close();
          responseLocal.Close();
          ServicePoint srvrPoint = ServicePointManager.FindServicePoint(fileUriLocal);
          MethodInfo ReleaseConns = srvrPoint.GetType().GetMethod
                  ("ReleaseAllConnectionGroups",
                  BindingFlags.Instance | BindingFlags.NonPublic);
          ReleaseConns.Invoke(srvrPoint, null);
      }

      catch (WebException r)
      {
          //Obtain more detail on the error;
          var response = (FtpWebResponse)r.Response;
          FtpStatusCode errorCode = response.StatusCode;
          string errorMessage = response.StatusDescription;
          MessageBox.Show(errorCode.ToString());
          MessageBox.Show("When getting file Dates: " + errorMessage.ToString());
      }         

      return tempIDPText;

0 个答案:

没有答案