主页 > 编程资料 > ASP.NET >
发布时间:2017-03-06 作者:网络 阅读:422次

在开发过程中需要进行证书的验证!

把浏览器中的证书导出为cer文件

using System;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Net.Security;
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// string url = "https://192.168.6.120/";
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
//Uri uri = new Uri("https://www.baifubao.com/");
Uri uri = new Uri("https://192.168.6.120/");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
X509Certificate cer = new X509Certificate("F:\\csharp2.cer");
request.ClientCertificates.Add(cer);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
Response.Write(reader.ReadToEnd());
//Console.Write(reader.ReadToEnd());
response.Close();
}
public static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
return false;
}
}

原文地址: http://blog.csdn.net/laoxiao1987/article/details/8332078


关键字词:

相关文章