WINHTTP忽略HTTPS证书

文章目录

        • WINHTTP忽略HTTPS证书
        • WINHTTP使用TLS协议版本

WINHTTP忽略HTTPS证书
发布了Wininet库忽略Https证书文章之后有朋友问我如果使用WINHTTP发起忽略证书的方法,
下列粘出代码可以看到跟Wininet是一样的不在过多赘述,不懂的朋友可以看我发的链接说明。

C++发起Https请求

#include 
#include 
#include 

#pragma comment(lib, "winhttp.lib")

#define TRANSFER_SIZE 1024

bool PerformHttpRequest() {
    HINTERNET hSession = WinHttpOpen(L"", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

    if (!hSession) {
        return false;
    }
    //lpszServerName 是IP或者域名
    LPCWSTR lpszServerName = L"192.168.99.99";
    //nServerPort 端口
    INTERNET_PORT nServerPort = 443;

    HINTERNET hConnect = WinHttpConnect(hSession, lpszServerName, nServerPort, 0);

    if (!hConnect) {
        WinHttpCloseHandle(hSession);
        return false;
    }

    HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/Login", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);

    if (!hRequest) {
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    BOOL bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

    if (!bResult && GetLastError() == ERROR_WINHTTP_SECURE_FAILURE) {
        //忽略所有异常Https CA证书问题
        // SECURITY_FLAG_IGNORE_ALL_CERT_ERRORS 也可以
        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;
        WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
        bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    }

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    bResult = WinHttpReceiveResponse(hRequest, NULL);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    wchar_t szBuff[TRANSFER_SIZE];
    DWORD dwReadSize;
    bResult = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, szBuff, &dwReadSize, WINHTTP_NO_HEADER_INDEX);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    wprintf(L"%ws\n", szBuff);

    DWORD dwBytesAvailable;
    bResult = WinHttpQueryDataAvailable(hRequest, &dwBytesAvailable);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    if (dwBytesAvailable > TRANSFER_SIZE) {
        wprintf(L"Data too long %d /b\n", GetLastError(), dwBytesAvailable);
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    DWORD dwBytesRead;
    ZeroMemory(szBuff, TRANSFER_SIZE);
    bResult = WinHttpReadData(hRequest, szBuff, dwBytesAvailable, &dwBytesRead);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    printf("%s\n", szBuff);

    WinHttpCloseHandle(hRequest);
    WinHttpCloseHandle(hConnect);
    WinHttpCloseHandle(hSession);

    return true;
}

int main() {
    if (PerformHttpRequest()) {
        return 0;
    }
    else {
        return 1;
    }
}

WINHTTP使用TLS协议版本
详细很多朋友使用WINHTTP是为了在低版本系统运行比如WIN7或2008这些系统使用TLS 1.0/1.1
无法使用Wininet库的TLS 1.2因此使用WINHTTP下面粘出代码是低版本修改了TLS的库代码
#include 
#include 
#include 

#pragma comment(lib, "winhttp.lib")

#define TRANSFER_SIZE 1024

bool PerformHttpRequest() {
    HINTERNET hSession = WinHttpOpen(L"", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

    if (!hSession) {
        return false;
    }
    // 设置要使用的协议 TLS 1.1,需要注意服务端也需要支持TLS 1.1否则无法使用
    DWORD dwFlags = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1;
    if (!WinHttpSetOption(hSession, WINHTTP_OPTION_SECURE_PROTOCOLS, &dwFlags, sizeof(dwFlags))) {
        WinHttpCloseHandle(hSession);
        return false;
    }
    //lpszServerName 是IP或者域名
    LPCWSTR lpszServerName = L"192.168.99.99";
    //nServerPort 端口
    INTERNET_PORT nServerPort = 443;

    HINTERNET hConnect = WinHttpConnect(hSession, lpszServerName, nServerPort, 0);

    if (!hConnect) {
        WinHttpCloseHandle(hSession);
        return false;
    }

    HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/Login", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);

    if (!hRequest) {
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    BOOL bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

    if (!bResult && GetLastError() == ERROR_WINHTTP_SECURE_FAILURE) {
        //忽略所有异常Https CA证书问题
        // SECURITY_FLAG_IGNORE_ALL_CERT_ERRORS 也可以
        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;
        WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
        bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    }

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    bResult = WinHttpReceiveResponse(hRequest, NULL);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    wchar_t szBuff[TRANSFER_SIZE];
    DWORD dwReadSize;
    bResult = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, szBuff, &dwReadSize, WINHTTP_NO_HEADER_INDEX);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    wprintf(L"%ws\n", szBuff);

    DWORD dwBytesAvailable;
    bResult = WinHttpQueryDataAvailable(hRequest, &dwBytesAvailable);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    if (dwBytesAvailable > TRANSFER_SIZE) {
        wprintf(L"Data too long %d /b\n", GetLastError(), dwBytesAvailable);
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    DWORD dwBytesRead;
    ZeroMemory(szBuff, TRANSFER_SIZE);
    bResult = WinHttpReadData(hRequest, szBuff, dwBytesAvailable, &dwBytesRead);

    if (!bResult) {
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return false;
    }

    printf("%s\n", szBuff);

    WinHttpCloseHandle(hRequest);
    WinHttpCloseHandle(hConnect);
    WinHttpCloseHandle(hSession);

    return true;
}

int main() {
    if (PerformHttpRequest()) {
        return 0;
    }
    else {
        return 1;
    }
}

你可能感兴趣的:(开发,c++,操作系统,https,网络协议,http)