获取"无效的网址"使用FtpWebRequest上传文件时

时间:2015-03-03 02:33:04

标签: c# ftp ftpwebrequest openvms

我们有一个OpenVMS(VMS)Alpha服务器,我需要访问它才能通过FTP传输文件。问题是它在启动连接时不支持FtpWebRequest中使用的命令(ftp://192.168.xx.xx),除了FtpWebRequest之外还有其他任何我可以使用的FTP功能吗?

我之前在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

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

网址没有表格

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

但是

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

请参阅https://en.wikipedia.org/wiki/URL