我需要通过客户端证书身份验证将一些 xmls 发布到 https 站点,但无法成功。
我有 2 个由提供商提供的 .pem 文件,如下所示:(我无法发送如此切割的所有数据)
证书.pem:
-----开始证书----- MIIC0DCCAjmgAwIBAgIKAd8CIHEBAwIEpjANBgkqhkiG9w0BAQUFADCBmTELMAkG
-----结束证书-----
密钥.pem:
-----开始 RSA 私钥----- MIICWwIBAAKBgQC+HN6jHJD1zoGLHYj1ycvg1yajll5zb3gExoWv7k+RbXLGuDEX
-----结束 RSA 私钥-----
我试图做的是
private static string HttpRequest(string url, string data)
{
HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);
//string privateKey = File.ReadAllText("c:\\key.pem");
//privateKey = privateKey.Replace("-----BEGIN RSA PRIVATE KEY-----", "");
//privateKey = privateKey.Replace("-----END RSA PRIVATE KEY-----", "");
//privateKey = privateKey.Replace("\n", "");
//Byte[] byteArr = Convert.FromBase64String(privateKey);
//How do I use below .pem files here to authentica
rq.ClientCertificates.Add(clientcert);
rq.Method = "POST";
rq.Proxy = null;
rq.ContentType = "application/www-form-urlencoded";
string dataToSend = data;
byte[] byteArray = Encoding.UTF8.GetBytes(dataToSend);
rq.ContentLength = byteArray.Length;
string responseFromServer = null;
try
{
Stream dataStream = rq.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse _WebResponse = rq.GetResponse();
dataStream = _WebResponse.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
catch (Exception ex)
{
}
return responseFromServer;
}