以下代码用于下载文件,但如果服务器具有自签名证书,则该代码不起作用:

DWORD errCode = 0;
HINTERNET intOpenHandle = InternetOpen("Snippet", LOCAL_INTERNET_ACCESS, NULL, 0, 0);
errCode = GetLastError();
if (intOpenHandle != NULL && errCode == ERROR_SUCCESS)
{
    HINTERNET urlOpenHandle = InternetOpenUrl(intOpenHandle, url, NULL, NULL, NULL, NULL);
    errCode = GetLastError();
    if (urlOpenHandle != NULL && errCode == ERROR_SUCCESS)
    {
        ...
有没有办法修改它以使用自签名证书?

最佳答案

InternetOpenUrl()InternetSetOptions()似乎是不可能的:后者需要打开的连接句柄,而不是前者提供的 session 句柄。
因此,我不得不使用更长的版本:

DWORD errCode = 0;
HINTERNET intOpenHandle = InternetOpen("Snippet", LOCAL_INTERNET_ACCESS, NULL, 0, 0);
if (intOpenHandle != NULL)
{
     HINTERNET httpRequest = HttpOpenRequest(intConnect, "GET", urlPath, NULL, NULL, NULL,
                                             INTERNET_FLAG_SECURE, 0);
     if (httpRequest != NULL)
     {
         DWORD dwFlags;
         DWORD dwBuffLen = sizeof(dwFlags);
         if (InternetQueryOption(httpRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, &dwBuffLen))
         {
             dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_WEAK_SIGNATURE |
                        SECURITY_FLAG_IGNORE_WRONG_USAGE;
             InternetSetOption(httpRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
         }
         if (HttpSendRequest(httpRequest, NULL, 0, NULL, 0))
         {
             ...

07-25 22:28