C#-使用HttpWebRequest传输到ftp站点

我正在尝试将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();


    }

我不确定上面的代码在做什么错.预先感谢您的协助.

解决方法:

为什么不改用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();
   }
 }
上一篇:python-使用fabric.operations.put()创建远程目录


下一篇:linux-使用wget和SFTP获取文件