Going to ftp using HttpWebRequest

I am trying to transfer an Excel file to the sftp site and my code is executing correctly, but I cannot see the file on the site.

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

I'm not sure what I am doing wrong in the code above. Thank you in advance for any help.

+3
source share
3 answers

Why don't you use FtpWebRequest instead?

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
source
  • The message does not put files. It sends data to server scripts.
  • Is this the full url? The domain name added with "http: //" does not make a valid URI, which must include the path and resource name.
  • "sftp" in the URL may suggest that the SSH file transfer protocol (SFTP) should be used, not FTP or HTTP
  • , FTP- ?
0

# 3

When you say "SFTP", you mean FTP over SSL or the SSH File Transfer Protocol. They require different approaches. If you really use SFTP, as in SSH File Transfer, I think you would be better off using a third-party library (if possible), such as sharpSSH. (Http://sshnet.codeplex.com/)

SFTP Wiki - http://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol

0
source

All Articles