获取“无效的网址”当使用FtpWebRequest上传文件时 [英] Getting "Invalid URL" when uploading file using FtpWebRequest

查看:151
本文介绍了获取“无效的网址”当使用FtpWebRequest上传文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个OpenVMS(VMS)Alpha服务器,我需要访问它才能通过FTP传输文件。问题是它不支持在启动连接时在 FtpWebRequest 中使用的命令(


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??

解决方案

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

这篇关于获取“无效的网址”当使用FtpWebRequest上传文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆