首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用WinHTTP与自签名证书进行SSL

如何使用WinHTTP与自签名证书进行SSL
EN

Stack Overflow用户
提问于 2013-10-13 03:38:18
回答 1查看 13.8K关注 0票数 10

我似乎对此有问题,本着可以被其他人参考的通用问题的精神,我正在寻找一个使用SSL的好例子。

更具体地说,我从WinHttpSendRequest得到错误0x00002F8F,它是ERROR_INTERNET_DECODING_FAILED (它告诉我这是一个cert错误)。我已经在这台机器上导入了证书,并且能够在IE中拉出页面,而不会出现证书错误。

The code I am using is here.

TLDR:如何将WinHTTP与自签名证书一起使用?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 04:45:03

对于WinHTTP,为了接受/允许SSL验证失败,您必须首先发出请求并允许其失败,然后禁用安全检查并重试请求句柄上的操作。大致是这样的:

代码语言:javascript
运行
复制
// Certain circumstances dictate that we may need to loop on WinHttpSendRequest
// hence the do/while
do
{
    retry = false;
    result = NO_ERROR;

    // no retry on success, possible retry on failure
    if(WinHttpSendRequest(
        mHRequest,
        WINHTTP_NO_ADDITIONAL_HEADERS,
        0,
        optionalData,
        optionalLength,
        totalLength,
        NULL
        ) == FALSE)
    {
        result = GetLastError();

        // (1) If you want to allow SSL certificate errors and continue
        // with the connection, you must allow and initial failure and then
        // reset the security flags. From: "HOWTO: Handle Invalid Certificate
        // Authority Error with WinInet"
        // http://support.microsoft.com/default.aspx?scid=kb;EN-US;182888
        if(result == ERROR_WINHTTP_SECURE_FAILURE)
        {
            DWORD dwFlags =
                SECURITY_FLAG_IGNORE_UNKNOWN_CA |
                SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
                SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
                SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;

            if(WinHttpSetOption(
                mHRequest,
                WINHTTP_OPTION_SECURITY_FLAGS,
                &dwFlags,
                sizeof(dwFlags)))
            {
                retry = true;
            }
        }
        // (2) Negotiate authorization handshakes may return this error
        // and require multiple attempts
        // http://msdn.microsoft.com/en-us/library/windows/desktop/aa383144%28v=vs.85%29.aspx
        else if(result == ERROR_WINHTTP_RESEND_REQUEST)
        {
            retry = true;
        }
    }
} while(retry);
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19338395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档