2015-03-03 313 views
2

我们有一个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; 
    } 

我得到上面的代码中的错误是“无效的网址......”。

而错误我收到的时候我尝试在浏览器中运行它: enter image description here

但我可以连接在Windows中使用通常的cmd命令: enter image description here

任何建议?

+0

*我得到上面的代码中的错误是“无效的URL .... “*:你问题中的代码不能抛出任何东西。向我们展示引发的实际代码(可能涉及'FtpWebRequest'的代码) – 2015-03-03 08:08:01

+0

另请参见http://stackoverflow.com/q/17306890/850848 – 2015-03-03 08:09:25

+0

@MartinPrikryl - 您在那里,代码已更新。 – NickSharp 2015-03-03 08:53:41

回答

3

的URL没有一种形式

ftp://192.168.xx.xx:FILE.TAB 

ftp://192.168.xx.xx/FILE.TAB 

https://en.wikipedia.org/wiki/URL

+0

这是棘手的,我已经做了之前,我改变了“主机+”:“+ remoteFile”到“主机+”/“+ remoteFile”,我仍然遇到同样的错误,但是当我把“/”地址本身的结尾(我更新了上面的代码),它工作正常!不知道为什么,但你的回答给了我这个想法,谢谢! – NickSharp 2015-03-03 10:31:59

相关问题