日期:2014-05-17  浏览次数:21397 次

SSL: SslStream, TcpClient
背景如下:
现有文件:clientKeyStore.jks,clientKeyStore.pfx,clientTrustStore.jks,clientTrustStore.pfx
通过这些证书文件部署ssl
使用 SslStream,TcpClient类 

已使用的命名空间
using System.Net;//HttpWebRequest类
using System.Net.Sockets;//TcpClient
using System.Security.Cryptography.X509Certificates;//证书文件类
using System.Web.Security;

需求:银联Webservice 
SSL 需要 “握手” 这个不会

然后通过HTTPS传送XML请求(这里的文档签名,已解决),接着...

------最佳解决方案--------------------
asp.net?
------其他解决方案--------------------
引用:
asp.net?

当然.net  java的google一大堆 可惜我不会JAVA 

只能通过SslStream,TcpClient

//创建一个TCP / IP客户端套接字
 TcpClient client = new TcpClient(machineName);

报错:服务器积极拒绝 远程一切正常
------其他解决方案--------------------
mark, tcp端口啥的没问题吧,没接触过ssl。。。
------其他解决方案--------------------
mark, tcp端口啥的没问题吧,没接触过ssl。。。
------其他解决方案--------------------

public class SslTcpClient
    {
        private static Hashtable certificateErrors = new Hashtable();
        //下面的方法调用由RemoteCertificateValidationDelegate。
        public static bool ValidateServerCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors)
        {
            ////此处报错 根据验证过程,远程证书无效。也就是这个地方了
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;

            //Console.WriteLine("Certificate error证书错误: {0}", sslPolicyErrors);
            return false;
        }

        public static void RunClient(string machineName, string serverName)
        {
            //创建一个TCP / IP客户端套接字
            TcpClient client = new TcpClient(machineName, 9090);
            
            //Console.WriteLine("Client connected.");
            //创建一个SSL流,将关闭客户端的流
            SslStream sslStream = new SslStream(
                client.GetStream(),
                false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate),