关于c#读取用户证书,并建立https连接
对实现https连接很陌生,因为需要建立ssl通讯,不会!
现在搞了份读证书的代码,但很多代码也不是很明白,希望高手能写个注释!感激,代码如下!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("OK");
StreamReader sr = null;
HttpWebResponse wr = null;
HttpWebRequest hp = (HttpWebRequest)WebRequest.Create("https://mch.tenpay.com/cgi-bin/refund.cgi");
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
Response.Write(hp.ClientCertificates.Count+"<br/>");
hp.ClientCertificates.Add(new X509Certificate2("C:\\Documents and Settings\\Administrator\\桌面\\机票解决方案5166\\1202952101.pfx", "1202952101"));
Response.Write(hp.ClientCertificates.Count + "<br/>");
wr = (HttpWebResponse)hp.GetResponse();
sr = new StreamReader(wr.GetResponseStream(), System.Text.Encoding.GetEncoding("GBK"));
this.TextBox1.Text = sr.ReadToEnd();
sr.Close();
wr.Close();
}
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}
public class MyPolicy : ICertificatePolicy
{
public bool CheckValidationResult(
ServicePoint srvPoint
, X509Certificate certificate
, WebRequest request
, int certificateProblem)
{
return true;
}
}
[解决办法]
关注下 帮顶了
[解决办法]
顶
[解决办法]