FTP Upload ftpWebRequest Proxy
Posted
by Rodney Vinyard
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Rodney Vinyard
Published on Fri, 11 Nov 2011 15:08:44 GMT
Indexed on
2011/11/11
17:54 UTC
Read the original article
Hit count: 477
Filed under:
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 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
//-----------------
//result: relPath = “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.
//-----------------
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 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
//-----------------
//result: relPath = “file.name”
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://proxy.server/" + relPath);
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
//-----------------
//user - password
//-----------------
//-----------------
// set proxy = null!
//-----------------
ftpWebRequest.Proxy = null;
//-----------------
// Copy the contents of the file to the request stream.
//-----------------
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);
© Geeks with Blogs or respective owner