嗨,我看到了使用节点 js 使用证书身份验证 API 的演示代码。
var https = require('https'), // Module for https
fs = require('fs'); // Required to read certs and keys
var options = {
hostname: 'xxx',
path: '/courses/tags?sortBy=0',
method: 'GET', // Method : Get or POST
key: fs.readFileSync('C://testNodeJs/key.pem'), //Input the directory for key.pem
cert: fs.readFileSync('C://testNodeJs/cert.pem') //Input the directory for cert.pem
//passphrase: 'InputPassWord' //Input the passphrase, please remember to put ',' End of Line for cert
};
makeAPICall = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
https.request(options, makeAPICall).end();
但是我们的项目只使用 c#,所以我想知道如何在 C# 中发送带有密钥和 Cert 证书的 Web 请求?我尝试下面的代码,但似乎不正确
var url1 = @"xxx";
HttpWebRequest request1 = WebRequest.Create(url1) as HttpWebRequest;
request1.Method = "GET";
request1.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate("key.pem"));
request1.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate("cert.pem"));
// Get response
using (HttpWebResponse response = request1.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
var result = reader.ReadToEnd();
}
它抛出异常“System.Security.Cryptography.CryptographicException:'找不到请求的对象”非常感谢