【问题标题】:"Requested URI is invalid" during upload with FtpWebRequest使用 FtpWebRequest 上传期间“请求的 URI 无效”
【发布时间】:2011-05-25 11:32:52
【问题描述】:

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

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

可能有什么问题?我试过关闭被动模式,但还是一样。

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();

}

【问题讨论】:

  • 该错误表明 12.22.44.45 上没有 ftp 服务器愿意接受 pokus 和密码的凭据(无论真实信息是什么)。您是否尝试使用这些设置/凭据在代码之外通过 ftp 到此服务器?
  • 你能告诉我们你从哪里得到错误信息吗?即是什么语句导致了错误?

标签: c# .net ftp ftpwebrequest


【解决方案1】:

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

这里是upload data

【讨论】:

    【解决方案2】:

    如果你想上传一些东西,你会have to provide the FTPClient with a filename

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 2020-06-16
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多