嘗試獲取某些SSL證書信息時遇到問題。C#將SSL證書寫入帶有「Forbidden」錯誤的X509Certificate
通常,代碼工作正常,但在某些情況下,我得到一個403 Forbidden錯誤。即使發生這種情況,我也想獲取證書信息。還有一些情況我也遇到了無效的登錄憑據。
無論上述任何一個問題如何,我都想獲取X509Certificate
對象。
如果我使用IE瀏覽它們,它會給我證書信息,無論消息如何。
所以對於示例代碼,既request
和response
被定義爲對象的變量,和request
正在呼叫getRequest()
之前已正確設置。
如果沒有網絡錯誤,此代碼正常工作,但如果出現錯誤,變量response
爲null
。
是否有另一種方法將X509Certificate
轉換爲名爲cert
的對象變量,即使看到這些錯誤?
我真的不在乎,如果我得到的答覆工作,我只關心X509Certificate
。
// callback used to validate the certificate in an SSL conversation
private static bool ValidateRemoteCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors policyErrors
) {
// allow any old dodgy certificate...
log.write(0, "entering ValidateRemoeCertificate ");
return true;
}
// getRequest
private int getRequest()
{
try {
log.write(1, "Validating " + webHostName);
// request.Method = method;
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
response = (HttpWebResponse)request.GetResponse();
response.Close();
} catch (Exception w) {
if (w.Message.Contains("401"))
{
log.write(4, "Web Site Authentication Required for " + host + ":" + port);
return 401;
}
else if (w.Message.Contains("403"))
{
log.write(4, "Forbidden Access for " + host + ":" + port);
return 403;
}
else
{
log.write(4, "Error Reading Web Site " + host + " Port " + port + " " + w.Message);
return 400;
}
}
return 0;
}
感謝您的協助!
謝謝,我會試一試......但它是有道理的。 – kooob
非常感謝你akton ...它的工作,現在正是我所需要的。非常感激! – kooob