quot;Requested URI is invalidquot; during upload with FtpWebRequest(“请求的 URI 无效在使用 FtpWebRequest 上传期间)
问题描述
我尝试将文件上传到 FTP 服务器上的目录.我将此方法与 FtpWebRequest
一起使用.我想为该用户上传一个文件到主目录,但我总是收到以下错误消息:
I trying upload file to a directory on a FTP server. I used this method with FtpWebRequest
.
I would like to upload one file to a home directory for this user, but I always get the following error message:
请求的 URI 对于此 FTP 命令无效.
The requested URI is invalid for this FTP command.
可能有什么问题?我试过关闭被动模式,但还是一样.
What can be problem? I tried use passive mode off, but it still the same.
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();
}
推荐答案
如果你想上传一些东西,你会 必须为 FTPClient 提供文件名.
If you want to upload something, you'll have to provide the FTPClient with a filename.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
这篇关于“请求的 URI 无效"在使用 FtpWebRequest 上传期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“请求的 URI 无效"在使用 FtpWebRequest 上传期间


基础教程推荐
- 将数据集转换为列表 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01