Windows Mobile ssl双向验证,调用Java端WebService

加载X509Certificate证书:

            // X509证书地址
            string certificateFile = "my.cer"
            FileStream fs = new FileStream(certificateFile, FileMode.Open, FileAccess.Read);
            byte[] certBytes = new byte[fs.Length];
            fs.Read(certBytes, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            X509Certificate cert = new X509Certificate(certBytes);
            // webService对象
            ws.ClientCertificates.Add(cert);

 其中my.cer不可以包含私钥,否则会报错:创建证书对象失败。所传入的数据不正确或 .NET Compact Framework 不支持该数据。.NET Compact Framework 不支持读取 pfx 文件。

 

确认证书对话框,始终返回true:

   public class TrustAllCertificatePolicy : ICertificatePolicy
    {
        public TrustAllCertificatePolicy()
        {
        }

        public bool CheckValidationResult(ServicePoint sp,
            X509Certificate cert, WebRequest req, int problem)
        {
            return true;
        }
    }

 调用WebService前,运行以下语句来调用TrustAllCertificatePolicy 类:

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

 

也可以像Web浏览器,弹出确认证书对话框,询问是否连接.

 

详见msdn:

http://msdn.microsoft.com/en-us/library/bb738067.aspx

你可能感兴趣的:(Windows Mobile)