首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HttpWebRequest传输到ftp站点

使用HttpWebRequest传输到ftp站点
EN

Stack Overflow用户
提问于 2012-05-17 01:49:52
回答 3查看 3.9K关注 0票数 3

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

代码语言:javascript
复制
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();
}

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

EN

回答 3

Stack Overflow用户

发布于 2012-05-17 01:51:43

为什么不使用FtpWebRequest呢?

代码语言:javascript
复制
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();
   }
 }
票数 4
EN

Stack Overflow用户

发布于 2012-05-17 01:59:49

  1. 帖子不放入文件。它将数据发送到服务器端脚本。
  2. 这是完整的网址吗?域名前面加上"http://“不能构成有效的URI,它必须包括路径和resource.
  3. "sftp”的名称可能会建议必须使用SSH文件传输协议,而不是FTP或HTTP
  4. 谁说上传到FTP资源将以这种方式工作?
票数 0
EN

Stack Overflow用户

发布于 2012-05-17 02:07:59

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

当你说"SFTP“时,你指的是FTP over SSL或SSH文件传输协议。它们需要不同的方法。如果你真的在使用SFTP,就像在SSH文件传输中一样,我认为你最好使用像sharpSSH这样的第三方库(如果可以)。(http://sshnet.codeplex.com/)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10623919

复制
相关文章

相似问题

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