Getting “Invalid URL” when uploading file using FtpWebRequest

随声附和 提交于 2019-12-10 11:05:28

问题


We have an OpenVMS (VMS) Alpha server which I need to access in order for me to transfer a file via FTP. The problem is that it does not support the command used in FtpWebRequest when initiating the connection (ftp://192.168.xx.xx), is there any other FTP function that I may used aside from FtpWebRequest?

I've been using my code on Windows and Unix environment before but this is my first time to do it on a VMS OS, I can also access the server via FTP using the command prompt.

Below is my code:

//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;
    }

The error I get on the above code is "Invalid URL....".

while the error I get when I try to run it on a browser:

But I can connect using the usual cmd command in windows:

Any suggestions??


回答1:


The URL does not have a form

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

but

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

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



来源:https://stackoverflow.com/questions/28823112/getting-invalid-url-when-uploading-file-using-ftpwebrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!