首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用FTPWebRequest上传期间“请求的URI无效”

使用FTPWebRequest上传期间“请求的URI无效”
EN

Stack Overflow用户
提问于 2019-01-04 05:20:47
回答 2查看 0关注 0票数 0

我尝试将文件上传到FTP服务器上的目录。我用这个方法FtpWebRequest。我想将一个文件上传到该用户的主目录,但我总是收到以下错误消息:

请求的URI对此FTP命令无效。

有什么问题?我试过关闭被动模式,但它仍然是一样的。

代码语言:javascript
复制
static void FtpUpload()
{


    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UsePassive = false;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential("pokus", "password");

    // Copy the contents of the file to the request stream.
    StreamReader sourceStream = new StreamReader(path);
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

    response.Close();

}
EN

回答 2

Stack Overflow用户

发布于 2019-01-04 13:28:22

我建议你使用WebClient,这是一个更高级别的抽象,并与HTTP和FTP一起使用,并且具有更简单的API和性能方面相同(使用相同的API)。

票数 0
EN

Stack Overflow用户

发布于 2019-01-04 15:06:30

如果要上传某些内容,则必须为FTPClient提供文件名。

代码语言:javascript
复制
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档