Searchable:
FTP
Upload ftpWebRequest Proxy
FTP command is not supported when using HTTP proxy
In the article below I will cover 2 topics
1. C# & Windows Command-Line FTP
Upload with No Proxy Server
2. C# & Windows Command-Line FTP
Upload with Proxy Server
Not covered here: Secure FTP / SFTP
Sample Attributes:
· UploadFilePath = “\\servername\folder\file.name”
· Proxy Server = “ftp://proxy.server/”
· FTP Target Server = ftp.target.com
· FTP User = “User”
· FTP Password = “Password”
with No Proxy Server
· Windows Command-Line
> ftp ftp.target.com
> ftp User: User
> ftp Password: Password
> ftp put \\servername\folder\file.name
> ftp dir
(result: file.name listed)
> ftp del file.name
> ftp dir
(result: file.name deleted)
> ftp quit
· C#
//-----------------
//Start FTP via _TargetFtpProxy
//-----------------
string relPath = Path.GetFileName(\\servername\folder\file.name);
//result: relPath = “file.name”
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp.target.com/file.name);
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
//-----------------
//user - password
//-----------------
ftpWebRequest.Credentials = new NetworkCredential("user, "password");
//-----------------
// set proxy = null!
//-----------------
ftpWebRequest.Proxy = null;
//-----------------
// Copy the contents of the file to the request stream.
//-----------------
StreamReader sourceStream = new StreamReader(“\\servername\folder\file.name”);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
ftpWebRequest.ContentLength = fileContents.Length;
//-----------------
// transer the stream stream.
//-----------------
Stream requestStream = ftpWebRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
//-----------------
// Look at the response results
//-----------------
FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();
Console.WriteLine("
Upload File Complete, status {0}", response.StatusDescription);
with Proxy Server
· Windows Command-Line
> ftp proxy.server
> ftp User:
[email protected]
> ftp Password: Password
> ftp put \\servername\folder\file.name
> ftp dir
(result: file.name listed)
> ftp del file.name
> ftp dir
(result: file.name deleted)
> ftp quit
· C#
//-----------------
//Start FTP via _TargetFtpProxy
//-----------------
string relPath = Path.GetFileName(\\servername\folder\file.name);
//result: relPath = “file.name”
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://proxy.server/" + relPath);
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
//-----------------
//user - password
//-----------------
ftpWebRequest.Credentials = new NetworkCredential("
[email protected], "password");
//-----------------
// set proxy = null!
//-----------------
ftpWebRequest.Proxy = null;
//-----------------
// Copy the contents of the file to the request stream.
//-----------------
StreamReader sourceStream = new StreamReader(“\\servername\folder\file.name”);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
ftpWebRequest.ContentLength = fileContents.Length;
//-----------------
// transer the stream stream.
//-----------------
Stream requestStream = ftpWebRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
//-----------------
// Look at the response results
//-----------------
FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();
Console.WriteLine("
Upload File Complete, status {0}", response.StatusDescription);