SSL-encrypted Web page grab (https), with a certificate validation

Because the project needs, need to send some data from the HTTPS page and get the data returned. Ordinary http pages easy to handle, with direct HttpWebRequest can be easily achieved. If the person is a HTTP web page but need to log in to view, but also difficult to achieve: use identity information to construct a grassroots HTTP protocol content HttpWebRequest plus, and then one round-trip to the server and then sends the extracted SessionID can handle. Now HTTPS + certificate is first encountered, the Chinese online query to the data rarely last online to find a solution in a foreign country, writing articles for easy viewing later ones!

Here is the code, not to explain!

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;

/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Class1
{
    private static int CERT_STORE_PROV_SYSTEM = 10;
    private static int CERT_SYSTEM_STORE_CURRENT_USER = (1 << 16);
    ///private static int CERT_SYSTEM_STORE_LOCAL_MACHINE = (2 << 16);

    [DllImport("CRYPT32", EntryPoint = "CertOpenStore", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr CertOpenStore(
        int storeProvider, int encodingType,
        int hcryptProv, int flags, string pvPara);

    [DllImport("CRYPT32", EntryPoint = "CertEnumCertificatesInStore", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr CertEnumCertificatesInStore(
        IntPtr storeProvider,
        IntPtr prevCertContext);

    [DllImport("CRYPT32", EntryPoint = "CertCloseStore", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern bool CertCloseStore(
        IntPtr storeProvider,
        int flags);

    X509CertificateCollection m_certs;

    public Class1()
    {
        m_certs = new 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 i = 0;
        while (currentCertContext != (IntPtr)0)
        {
            m_certs.Insert(i++, new X509Certificate(currentCertContext));
            currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);
        }
        CertCloseStore(storeHandle, 0);
    }

    public 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 i = 0;
        while (currentCertContext != (IntPtr)0)
        {
            m_certs.Insert(i++, new X509Certificate(currentCertContext));
            currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);
        }
        CertCloseStore(storeHandle, 0);

        return m_certs.Count;
    }

    public X509Certificate this[int index]
    {
        get
        {
            // Check the index limits.
            if (index < 0 || index > m_certs.Count)
                return null;
            else
                return m_certs[index];
        }
    }
}


---------------------------------------------------------------------------


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.Text;
using System.Net;
using System.IO;

using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
    public AcceptAllCertificatePolicy()
    {
    }

    public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb)
    {
        return true;
    }
}

public partial class _Default : System.Web.UI.Page
{
    protected static string cookieHeader;

    protected void Page_Load(object sender, EventArgs e)
    {
    }


    protected void btnSend_Click(object sender, EventArgs e)
    {

        ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();


        = @ postData String "CREDITID = CDB992362323";
        Encoding = Encoding.GetEncoding encoding ( "GB2312");
        String strUrl = https://www.server.com/asdfjlsdf.jsp ;
        byte [] = Data encoding.GetBytes (postData) ;
        // prepare the request ...  
        the HttpWebRequest myRequest = (the HttpWebRequest) WebRequest.Create (strUrl);
        myRequest.ClientCertificates.Add (S [0]); // certificate can traverse out to allow customers to choose, but here is my test program, directly on the choice that I need.
        = myRequest.Method "the POST";
        myRequest.ContentType = "file application / X-WWW-form-urlencoded";
        myRequest.ContentLength = data.length;
        myRequest.KeepAlive = to true;

        Stream newStream = myRequest.GetRequestStream();
        //发送数据  
        newStream.Write(data, 0, data.Length);
        newStream.Close();

        //读取回来的数据
        HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
        txtAccept.Text = sr.ReadToEnd();
        sr.Close();
        res.Close();  

        return;
       }

}

Reproduced in: https: //www.cnblogs.com/ziyang/archive/2008/07/10/1240089.html

Guess you like

Origin blog.csdn.net/weixin_34130389/article/details/93161290