65.9K
CodeProject 正在变化。 阅读更多。
Home

将数字证书(公钥)附加到 HTTPS 请求

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.44/5 (16投票s)

2008年8月6日

CPOL

1分钟阅读

viewsIcon

103717

downloadIcon

1933

从 Windows 应用程序 (.NET) 将数据发布到 HTTPS(安全连接)URL,并附带数字证书,然后获取响应。

引言

本文将指导您如何从 Windows 应用程序 (.NET) 将数据发布到 HTTPS(即安全连接)URL,方法是附带来自证书文件的数字证书,并获取响应。代码是用 C# 编写的。

背景

阅读本文不需要特殊的背景知识。任何初级或中级程序员都能够理解这段代码。

Using the Code

您应该拥有一个有效的证书文件,您将使用该文件通过附加该证书将数据发布到安全的网站/Web 服务。实际上,您将通过这种方式将公钥附加到您的 HTTPS 请求。另一种方法是遍历安装在您 PC 上的所有证书,从存储列表中获取正确的证书,然后将公钥附加到您的 HTTPS 请求。您可以使用这两种方法中的任何一种。但在这里,我将使用第一种方法。

//
private void postFile()
{
   HttpWebRequest req = null;
   HttpWebResponse rsp = null;
  
   X509Certificate cert = X509Certificate.CreateFromCertFile("d:\\cert\\abc.crt");
   // Create a  X509Certificat object from yor certificate.
   // other way is to go through all the cerificates  which are installed
   // in your Pc and get the right one from the store list

   string uri = "https://abc.com:2111/test.aspx"; 

   // A url which is looking for the right public key with 
   // the incomming https request

    String myfile = File.ReadAllText("C:\\somfile.xml");

    req = (HttpWebRequest)System.Net.WebRequest.Create(uri);

    String DataToPost = this.GetTextFromXMLFile(myfile);
   
    String strSenderID = "123";

    req.Method = "POST";        // Post method
    req.ContentType = "application/octet-stream";   // content type
    //You can also use ContentType = "text/xml";
   
    req.Headers.Add("sender-id", strSenderID);  
   // Some Header information which you would like to send 
   // with the request
    req.ContentLength = 1000; 
    req.KeepAlive = false;
    req.UserAgent = null;
    req.Timeout = 99999;
    req.ReadWriteTimeout = 99999;
    req.ServicePoint.MaxIdleTime = 99999;

    req.ClientCertificates.Add(cert);
    // Attaching the Certificate To the request

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

    // when you browse manually you get a dialogue box asking 
    // that whether you want to browse over a secure connection.
    // this line will suppress that message
    //(pragramatically saying ok to that message). 

    StreamWriter writer = new StreamWriter(req.GetRequestStream());

    writer.WriteLine(this.GetTextFromXMLFile(myfile));

    writer.Close();

    rsp = (HttpWebResponse)req.GetResponse();

    System.IO.StreamReader reader = 
           new System.IO.StreamReader(rsp.GetResponseStream());
    String retData = reader.ReadToEnd();

    if (req != null) req.GetRequestStream().Close();
    if (rsp != null) rsp.GetResponseStream().Close();
  
}

此函数将读取文件的内容并返回文件内容。

//
private string GetTextFromXMLFile(string file)  // this 
{
    StreamReader reader = new StreamReader(file);
    string ret = reader.ReadToEnd();
    reader.Close();
    return ret;
}//

函数 TrustAllCertificatePolicy() 将捕获自定义证书策略的证书策略异常。

//
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
    public TrustAllCertificatePolicy()
    { }
    public bool CheckValidationResult(ServicePoint sp, 
       System.Security.Cryptography.X509Certificates.
        X509Certificate   cert, WebRequest req, int problem)
    {

        return true;
    }
}
//

关注点

密码学是一个非常大的研究领域,在这里我们只是讨论了它的一小部分。我将很快更新本文,提供最新的源代码。

请查看我的另一篇文章:在 Oracle 中使用 Crystal Reports 和参数化查询(将 SQL 查询参数传递给 Crystal Reports)

© . All rights reserved.