Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done
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 User: User
> ftp Password: Password
> 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: User@ftp.target.com
> ftp Password: Password
> 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("user@ftp.target.com, "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);
Posted on Friday, November 11, 2011 9:08 AM FTP , FTP | Back to top


Comments on this post: FTP Upload ftpWebRequest Proxy

# kids air max shoes,cheap nike air max 2011
Requesting Gravatar...
There are a lot of changes being made now and you'll see rolex more in the near future as well.
Left by kids air max shoes,cheap nikes on Dec 16, 2011 2:23 AM

# re: FTP Upload ftpWebRequest Proxy
Requesting Gravatar...
How to replace the cartridges / ink box?
① printer is powered on, press and hold the the CARTRIDGE key, until the ON LINE indicator starts blinking; ② open the front cover, holding the marker on the edge of the BJ cartridge, then BJ cartridge from the printer to lift and unload; toggle ink box on the hook, lift the ink cartridge. ③ will be a new BJ cartridge diagonally embedded in the cartridge holder;, or embed a new ink cartridge in the cartridge. ④ Close the front cover, press the the CARTRIDGE key, wait, the inkjet head cleaning is complete and the printer returns to the initial state.pg-510
Left by britanniainks on Jan 15, 2013 1:55 AM

# re: FTP Upload ftpWebRequest Proxy
Requesting Gravatar...
with windows command line it is working fine

with the cSharp coding i am getting below error
The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.
Left by ravi on Feb 19, 2013 4:02 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net