FTPs encrypted uploads

Source: Internet
Author: User

The company requires that the FTP interface cannot be transmitted in clear text, so the ADC system will increase the FTPS mode, but there are many ways to find the network can not be achieved
Used a
Method One

FtpWebRequest request = (ftpwebrequest) webrequest.create (Fileuploadpath);
Request. Method = WebRequestMethods.Ftp.ListDirectory;
Request. Credentials = new NetworkCredential (UPLOADUSER,UPLOADPWD);

Request. Enablessl = true;
Request. Usepassive = true;
Servicepointmanager.servercertificatevalidationcallback = Delegate (object sender, X509Certificate certificate, X509chain chain, Sslpolicyerrors sslpolicyerrors)
{return true;};

FtpWebResponse response = (ftpwebresponse) request. GetResponse ();
Stream responsestream = null;
StreamReader readstream = null;
Try
{
Responsestream = Response. GetResponseStream ();
Readstream = new StreamReader (Responsestream, System.Text.Encoding.UTF8);
if (readstream! = null)
Console.WriteLine (Readstream.readtoend ());
}
Finally
{
if (readstream! = null)
Readstream.close ();
if (response! = NULL)
Response. Close ();
}

Method Two
public void Upload (string filename)
{

FileInfo Fileinf = new FileInfo (filename);
String uri = "ftp://" + Ftpserverip + "/" + fileinf.name;
FtpWebRequest reqftp;

To create a FtpWebRequest object from a URI
Reqftp = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + Ftpserverip + "/" + Fileinf.name));

FTP User name and password
Reqftp.credentials = new NetworkCredential (Ftpuserid, FTPPassword);
Ssl
Reqftp.enablessl = true;
The default is true and the connection is not closed
is executed after a command
Reqftp.keepalive = false;

Specify what commands to execute
Reqftp.method = WebRequestMethods.Ftp.UploadFile;

Specifying data Transfer Types
Reqftp.usebinary = true;
Specify transfer Mode
Reqftp.usepassive =false;

Notifies server file size when uploading a file
Reqftp.contentlength = Fileinf.length;

Buffer size set to 2KB
int bufflength = 2048;

byte[] buff = new Byte[bufflength];
int Contentlen;

Open a file stream (System.IO.FileStream) to read the uploaded file
FileStream fs = Fileinf.openread ();
Try
{
Write the uploaded file to the stream
Stream strm = Reqftp.getrequeststream ();

2KB per read file stream
Contentlen = fs. Read (Buff, 0, bufflength);

Stream content does not end
while (Contentlen! = 0)
{
Write content from file stream to upload stream
Strm. Write (Buff, 0, Contentlen);

Contentlen = fs. Read (Buff, 0, bufflength);
}

Close two streams
Strm. Close ();
Fs. Close ();
}
catch (Exception ex)
{
MessageBox.Show (ex. Message, "Upload Error");
}
}

It's all about the remote link failure. Also mention that our server is not required to verify the port number of the certificate is 9,901 fixed if the passive mode to log on
Can you please help me see what improvements or direct cases to provide

FTPs encrypted uploads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.