c# - 使用FtpWebRequest上传文件时获取 "Invalid URL"

标签 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;
    }

我在上面的代码中遇到的错误是“无效的 URL....”。

当我尝试在浏览器上运行它时出现错误: enter image description here

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

有什么建议吗?

最佳答案

URL 没有格式

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

但是

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

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

关于c# - 使用FtpWebRequest上传文件时获取 "Invalid URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28823112/

相关文章:

c - exec ftp -vn 系统返回错误

azure - Azure Web App 的 FTP 发布失败

FTP - 550 无法更改目录

c# - 如何判断 FTP 目录是否存在

vb.net - 在 FTP 中创建目录

c# - 使用 IEnumerable 检测修改

c# - 通过反射获取方法失败

c# - 自定义实体 View 模型映射器的循环依赖

c# - ASP.NET C#通过FTPwebRequest上传MemoryStream内容问题

c# - 如何使用 C# 驱动程序在 mongodb 中使用 $currentdate