0

我在尝试获取一些 SSL 证书信息时遇到问题。

通常,代码工作正常,但在某些情况下,我收到 403 Forbidden 错误。即使发生这种情况,我也想获取证书信息。在某些情况下,我也获得了无效的登录凭据。

X509Certificate无论这些问题如何,我都想获得该对象。

如果我使用 IE 浏览它们,它会为我提供证书信息,而不管消息如何。

因此对于示例代码,两者requestresponse都定义为对象变量,并且request在调用getRequest().

如果没有 web 错误,此代码可以正常工作,但如果有错误,则变量responsenull.

是否有另一种方法可以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;
}

感谢您的任何帮助!

4

1 回答 1

1

请改用SslStream类。使用接受RemoteCertificateValidationCallback委托的构造函数,该委托在AuthenticateAsClient期间调用以检查服务器的证书。

于 2012-09-30T07:15:28.773 回答