ここで説明するのではない、コードです!
システムを使用しました。
System.Dataを使用しました。
System.Configurationを使用しました。
System.Webのを使用して、
System.Web.Securityを使用しました。
システムを使用しました。
System.Netを使用しました。
System.IOを使用しました。
System.Textのを使用しました。
System.Security.Cryptographyを使用しました。
System.Security.Cryptography.X509Certificatesを使用しました。
System.Runtime.InteropServicesを使用しました。
/// <要約>
///のClass1的摘要说明
/// </要約>
パブリッククラスClass1
{
プライベート静的int型のCERT_STORE_PROV_SYSTEM = 10。
プライベート静的int型CERT_SYSTEM_STORE_CURRENT_USER =(1 << 16)。
///プライベート静的int型のCERT_SYSTEM_STORE_LOCAL_MACHINE =(2 << 16)。
[DLLIMPORT( "CRYPT32"、エントリーポイント= "CertOpenStore"のCharSet = CharSet.Unicode、SetLastError =真)]
パブリック静的にexternのIntPtr CertOpenStore(
INT storeProvider、INT encodingType、
INT hcryptProv、INTフラグ、文字列pvPara)。
[DLLIMPORT( "CRYPT32"、エントリーポイント= "CertEnumCertificatesInStore"のCharSet = CharSet.Unicode、SetLastError =真)]
パブリック静的にexternのIntPtr CertEnumCertificatesInStore(
のIntPtr storeProvider、
のIntPtr prevCertContext)。
[DLLIMPORT( "CRYPT32"、エントリーポイント= "CertCloseStore"のCharSet = CharSet.Unicode、SetLastError =真)]
パブリック静的にextern BOOL CertCloseStore(
のIntPtr storeProvider、
フラグINT)。
X509CertificateCollectionのm_certs。
パブリックのClass1()
{
m_certs =新しいX509CertificateCollection()。
IntPtr storeHandle;
storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM、0、0、CERT_SYSTEM_STORE_CURRENT_USER、 "MY")。
IntPtr currentCertContext;
currentCertContext = CertEnumCertificatesInStore(storeHandle、(のIntPtr)0)。
int型私= 0;
しばらく(currentCertContext =(のIntPtr)0!)
{
m_certs.Insert(I ++は、新しいのX509Certificate(currentCertContext));
currentCertContext = CertEnumCertificatesInStore(storeHandle、currentCertContext)。
}
CertCloseStore(storeHandle、0);
}
公共INTは、init()
{
のIntPtr storeHandle。
storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM、0、0、CERT_SYSTEM_STORE_CURRENT_USER、 "MY")。
IntPtr currentCertContext;
currentCertContext = CertEnumCertificatesInStore(storeHandle、(のIntPtr)0)。
int型私= 0;
しばらく(currentCertContext =(のIntPtr)0!)
{
m_certs.Insert(I ++は、新しいのX509Certificate(currentCertContext));
currentCertContext = CertEnumCertificatesInStore(storeHandle、currentCertContext)。
}
CertCloseStore(storeHandle、0);
m_certs.Countを返します。
}
公共のX509Certificateこの[int型のインデックス]
{
取得
{
//インデックスの制限を確認してください。
(インデックス<0 ||インデックス> m_certs.Count)であれば
、リターンはnull;
それ以外の
戻りm_certs [インデックス]。
}
}
}
-------------------------------------------------- -------------------------
システムを使用しました。
System.Dataを使用しました。
System.Configurationを使用しました。
System.Webのを使用して、
System.Web.Securityを使用しました。
たSystem.Web.UIを使用しました。
System.Web.UI.WebControlsを使用しました。
System.Web.UI.WebControls.WebPartsを使用しました。
System.Web.UI.HtmlControlsを使用しました。
System.Textのを使用しました。
System.Netを使用しました。
System.IOを使用しました。
System.Security.Cryptographyを使用しました。
System.Security.Cryptography.X509Certificatesを使用しました。
System.Runtime.InteropServicesを使用しました。
内部クラスAcceptAllCertificatePolicy:ICertificatePolicy
{
パブリックAcceptAllCertificatePolicy()
{
}
パブリックブールCheckValidationResult(のServicePoint SPOINT、X509Certificateの証明書、WebRequestクラスwRequest、INT certProb)
{
trueを返します。
}
}
公共の部分クラスの_Default:System.Web.UI.Page
{
保護された静的な文字列cookieHeader。
保護されたボイドをPage_Load(オブジェクト送信者、のEventArgs電子)
{
}
保護されたボイドbtnSend_Click(オブジェクト送信者、のEventArgs電子)
{
ServicePointManager.CertificatePolicy =新しいAcceptAllCertificatePolicy();
= @ POSTDATA文字列"CREDITID = CDB992362323";
エンコード= Encoding.GetEncodingエンコーディング( "2312");
文字列strUrl = https://www.server.com/asdfjlsdf.jsp ;
バイト[] =データencoding.GetBytes(POSTDATA) ;
//リクエストを準備...
HttpWebRequestのmyRequest =(HttpWebRequestの)WebRequest.Create(strUrl);
myRequest.ClientCertificates.Addは(S [0]); // 証明書は、顧客が選択できるように出て横断しますが、ここに私のテストプログラムであることができ、直接、私は必要な選択に。
= myRequest.Method "POST";
myRequest.ContentType = "ファイルアプリケーション/ x-www-form-urlencodedで";
myRequest.ContentLength = data.length;
myRequest.KeepAlive = trueに。
ストリームnewstreamに= myRequest.GetRequestStream()。
//发送数据
newStream.Write(データ、0、data.Length)。
newStream.Close();
//读取回来的数据
HttpWebResponseのRES =(HttpWebResponseの)myRequest.GetResponse();
StreamReaderをSR =新しいStreamReaderを(res.GetResponseStream()、System.Text.Encoding.Default)。
txtAccept.Text = sr.ReadToEnd()。
sr.Close();
res.Close();
返します。
}
ます。https://www.cnblogs.com/ziyang/archive/2008/07/10/1240089.htmlで再現