c#连接带自签名证书的FTP

 

    由于FTP传输是用明文形式,故客户要求加密,所以研究了下连接带证书FTP的情况,在网上搜索了下资料,发现在一个英文网站上得到了启发(http://blogs.msdn.com/b/adarshk/archive/2005/04/22/410925.aspx),经过研究,已能正常连接带证书的FTP,把测试代码贴上,以让后来者有所帮助

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Net.Security;

 

namespace FTPForSSLTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 验证证书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="certificate"></param>
        /// <param name="chain"></param>
        /// <param name="sslPolicyErrors"></param>
        /// <returns></returns>
        public static bool ValidateServerCertificate(object sender,X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors)
        {
            //return true;
            if (sslPolicyErrors ==
            SslPolicyErrors.RemoteCertificateChainErrors)
            {
                return false;
            }
            else if (sslPolicyErrors ==
            SslPolicyErrors.RemoteCertificateNameMismatch)
            {
                System.Security.Policy.Zone z =
                   System.Security.Policy.Zone.CreateFromUrl
                   (((FtpWebRequest)sender).RequestUri.ToString());
                if (z.SecurityZone ==System.Security.SecurityZone.Internet)
                {
                    return true;
                }
                return false;
            }
            return true;

        }

        string ftpPath = "路径
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
                ftpRequest.EnableSsl = false;
                ftpRequest.UseBinary = true;
                ftpRequest.Credentials = new NetworkCredential("jiangxk", "123456");
                if (File.Exists(Application.StartupPath + "/" + "QC_RMS_201202231200.csv"))
                {
                    File.Delete(Application.StartupPath + "/" + "QC_RMS_201202231200.csv");
                }
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                ftpRequest.EnableSsl = true;
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
                FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                Stream stream = ftpResponse.GetResponseStream();
                long dataLong = ftpResponse.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = stream.Read(buffer, 0, bufferSize);
                FileStream fs = new FileStream(Application.StartupPath + "/" + "QC_RMS_201202231200.csv", FileMode.Create);
                while (readCount > 0)
                {
                    fs.Write(buffer, 0, readCount);
                    readCount = stream.Read(buffer, 0, bufferSize);
                }
                stream.Close();
                fs.Close();
                ftpResponse.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值