我們有一個OpenVMS(VMS)Alpha服務器,我需要訪問它才能通過FTP傳輸文件。問題是它不支持在啓動連接時使用的FtpWebRequest
命令(ftp://192.168.xx.xx),除了FtpWebRequest之外,還有其他的FTP功能嗎?使用FtpWebRequest上傳文件時獲取「無效的URL」
我一直在Windows和Unix環境中使用我的代碼,但這是我第一次在VMS操作系統上執行它,我也可以使用命令提示符通過FTP訪問服務器。
下面是我的代碼:
//Initializing ftp request
ftp ftpClient = new ftp(@"ftp://192.168.xx.xx/", "username", "password");
MessageBox.Show((ftpClient.upload("FILE.TAB", @"C:\FILE.TAB")).ToString());
public ftp(string hostIP, string userName, string password)
{
host = hostIP; user = userName; pass = password;
}
public string upload(string remoteFile, string localFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
///* When in doubt, use these options */
ftpRequest.UseBinary = false;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpRequest.GetRequestStream();
/* Open a File Stream to Read the File for Upload */
FileStream localFileStream = new FileStream(localFile, FileMode.Open);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
/* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
return "0";
}
catch (Exception ex) { return ex.ToString(); }
//return 1;
}
我得到上面的代碼中的錯誤是「無效的網址......」。
而錯誤我收到的時候我嘗試在瀏覽器中運行它:
但我可以連接在Windows中使用通常的cmd命令:
任何建議?
*我得到上面的代碼中的錯誤是「無效的URL .... 「*:你問題中的代碼不能拋出任何東西。向我們展示引發的實際代碼(可能涉及'FtpWebRequest'的代碼) – 2015-03-03 08:08:01
另請參見http://stackoverflow.com/q/17306890/850848 – 2015-03-03 08:09:25
@MartinPrikryl - 您在那裏,代碼已更新。 – NickSharp 2015-03-03 08:53:41