Guest User

Untitled

a guest
Jun 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. > Unable to read data from the transport
  2. > connection: A connection attempt
  3. > failed because the connected party did
  4. > not properly respond after a period of
  5. > time, or established connection failed
  6. > because connected host has failed to
  7. > respond.
  8.  
  9. ...
  10.  
  11. private void CreateDownloadFile()
  12. {
  13. _OutputFile = new FileStream(_SourceFile, FileMode.Create);
  14. }
  15. public string FTPDownloadFile()
  16. {
  17. this.CreateDownloadFile();
  18.  
  19. myReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(this.DownloadURI));
  20. myReq.Method = WebRequestMethods.Ftp.DownloadFile;
  21. myReq.UseBinary = true;
  22. myReq.Credentials = new NetworkCredential(_ID, _Password);
  23.  
  24. FtpWebResponse myResp = (FtpWebResponse)myReq.GetResponse();
  25. Stream ftpStream = myResp.GetResponseStream();
  26.  
  27. int bufferSize = 2048;
  28. int readCount;
  29. byte[] buffer = new byte[bufferSize];
  30. int bytesRead = 0;
  31. readCount = ftpStream.Read(buffer, 0, bufferSize);
  32.  
  33. while (readCount > 0)
  34. {
  35. _OutputFile.Write( buffer, 0, readCount );
  36. readCount = ftpStream.Read( buffer, 0, bufferSize );
  37.  
  38. Console.Write( '.' ); // show progress on the console
  39. bytesRead += readCount;
  40. }
  41. Console.WriteLine();
  42. logger.logActivity( " FTP received " + String.Format( "{0:0,0}", bytesRead ) + " bytes" );
  43.  
  44. ftpStream.Close();
  45. _OutputFile.Close();
  46. myResp.Close();
  47. return this.GetFTPStatus();
  48. }
  49.  
  50. public string GetFTPStatus()
  51. {
  52. return ((FtpWebResponse)myReq.GetResponse()).StatusDescription;
  53. }
  54.  
  55. FtpWebRequest reqFTP;
  56.  
  57. string fileName = @"c:downloadDirlocalFileName.txt";
  58. FileInfo downloadFile = new FileInfo(fileName);
  59. string uri = "ftp://ftp.myftpsite.com/ftpDir/remoteFileName.txt";
  60.  
  61.  
  62. FileStream outputStream = new FileStream(fileName, FileMode.Append);
  63.  
  64. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  65. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  66. reqFTP.UseBinary = true;
  67. reqFTP.KeepAlive = false;
  68. reqFTP.Timeout = -1;
  69. reqFTP.UsePassive = true;
  70. reqFTP.Credentials = new NetworkCredential("userName", "passWord");
  71. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  72. Stream ftpStream = response.GetResponseStream();
  73. long cl = response.ContentLength;
  74. int bufferSize = 2048;
  75. int readCount;
  76. byte[] buffer = new byte[bufferSize];
  77. readCount = ftpStream.Read(buffer, 0, bufferSize);
  78. Console.WriteLine("Connected: Downloading File");
  79. while (readCount > 0)
  80. {
  81. outputStream.Write(buffer, 0, readCount);
  82. readCount = ftpStream.Read(buffer, 0, bufferSize);
  83. Console.WriteLine(readCount.ToString());
  84. }
  85.  
  86. ftpStream.Close();
  87. outputStream.Close();
  88. response.Close();
  89. Console.WriteLine("Downloading Complete");
Add Comment
Please, Sign In to add comment