我需要發送一些xmls到客戶端證書身份驗證的https站點,但無法成功完成。HttpWebRequest客戶端身份驗證
我從供應商提供的類似下面2的.pem文件:(我不能發送所以板缺的所有數據)
cert.pem:
----- BEGIN CERTIFICATE --- - MIIC0DCCAjmgAwIBAgIKAd8CIHEBAwIEpjANBgkqhkiG9w0BAQUFADCBmTELMAkG
----- END CERTIFICATE -----
key.pem:
-----乙EGIN RSA私鑰----- MIICWwIBAAKBgQC + HN6jHJD1zoGLHYj1ycvg1yajll5zb3gExoWv7k + RbXLGuDEX
----- END 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;
}
嗨,謝謝你的回答,但它不起作用。當我查看System.New跟蹤日誌時它嘗試查找cert.pem私鑰然後未能找到它,因此拋出異常 – Yucel