【问题标题】:Transferring to ftp site using HttpWebRequest使用 HttpWebRequest 传输到 ftp 站点
【发布时间】:2012-05-16 17:49:51
【问题描述】:

我正在尝试将 Excel 文件传输到 sftp 站点,并且我的代码可以正常执行,但我在站点上看不到该文件。

private static void SendFile(string FileName)
{
    FileStream rdr = new FileStream(FileName + ".csv", FileMode.Open);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://sftp.somesite.com");
    HttpWebResponse resp;
    req.Method = "Post";
    req.Credentials = new NetworkCredential("UN", "PW", "Domain");

    req.ContentLength = rdr.Length;
    req.AllowWriteStreamBuffering = true;
    Stream reqStream = req.GetRequestStream();
    byte[] inData = new byte[rdr.Length];
    int bytesRead = rdr.Read(inData, 0, Convert.ToInt32(rdr.Length));

    reqStream.Write(inData, 0, Convert.ToInt32(rdr.Length));
    rdr.Close();
}

我在上面的代码中做错了什么?

【问题讨论】:

    标签: c# .net visual-studio ftp sftp


    【解决方案1】:

    你为什么不改用 FtpWebRequest?

    using System.Net;
    using System.IO;
    
    public class Ftp
    {
      private static void ftpUpload(string filename, string destinationURI)
      {
            FileInfo fileInfo = new FileInfo(filename);
            FtpWebRequest reqFTP = CreateFtpRequest(new Uri(destinationURI));
    
            reqFTP.KeepAlive = false;
    
            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    
            // use binary 
            reqFTP.UseBinary = true;
    
            reqFTP.ContentLength = fileInfo.Length;
    
            // Buffer size set to 2kb
            const int buffLength = 2048;
            byte[] buff = new byte[buffLength];
    
            // Stream to which the file to be upload is written
            Stream strm = reqFTP.GetRequestStream();
    
            FileStream fs = fileInfo.OpenRead();
    
            // Read from the file stream 2kb at a time
            int cLen = fs.Read(buff, 0, buffLength);
    
            // Do a while till the stream ends
            while (cLen != 0)
            {
                // FTP Upload Stream
                strm.Write(buff, 0, cLen);
                cLen = fs.Read(buff, 0, buffLength);
            }
    
            // Close 
            strm.Close();
            fs.Close();
       }
     }
    

    【讨论】:

    • 我也想过同样的事情,但我收到以下错误:“无法将'System.Net.HttpWebRequest'类型的对象转换为'System.Net.FtpWebRequest'”
    • CreateFTP 请求是什么命名空间?我尝试按如下方式进行转换,但仍然收到相同的错误: FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri("sftp.somesite.com" + "/" + Path.GetFileName(filename)));
    【解决方案2】:
    1. Post 不放置文件。它将数据发送到服务器端脚本。
    2. 这是完整的 URL 吗?以“http://”开头的域名不是有效的 URI,它必须包含路径和资源名称。
    3. URL 中的“sftp”可能表明必须使用 SSH 文件传输协议 (SFTP),而不是 FTP 或 HTTP
    4. 谁说上传到 FTP 资源会这样工作?

    【讨论】:

    • @Eric 这真的取决于你真正拥有的服务器 - FTP、SFTP 或 HTTP。一旦弄清楚这一点,您就可以编写正确的代码。否则它会在黑暗的房间里寻找一只根本不存在的黑猫。
    【解决方案3】:

    正如尤金在#3中所说的那样

    当您说“SFTP”时,您是指基于 SSL 的 FTP 还是 SSH 文件传输协议。他们需要不同的方法。如果您确实在使用 SFTP,例如在 SSH 文件传输中,我认为您最好使用第三方库(如果可以的话),例如 sharpSSH。(http://sshnet.codeplex.com/)

    SFTP 上的维基 - http://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol

    【讨论】:

    • 我相信它是 sftp 从我给出的 url 判断。除了使用第三方库,还有其他方法吗?
    • 不幸的是,当我最后一次不得不使用 .NET 进行 SFTP 传输时(7 个月前),我没有找到来自 MS 的任何内置支持。两个第 3 方库看起来不错(也可以查看 SSH.NET [sshnet.codeplex.com/])。
    猜你喜欢
    • 2012-02-07
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多