大家好,又见面了,我是你们的朋友全栈君。
#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, “winhttp”)
struct callback_param_t
{
HINTERNET hInet;
DWORD dwErrCert;
};
static VOID CALLBACK SyncCallback(HINTERNET, DWORD_PTR, DWORD, PVOID, DWORD);
DWORD ConnectHTTPSFunc(LPCWSTR pswzServerName,LPCWSTR pswzObjectName,LPDWORD lpdwErrCert)
{
DWORD dwErr = ERROR_SUCCESS;
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
if(NULL == lpdwErrCert)
{
*lpdwErrCert = 0;
}
hSession = ::WinHttpOpen(0,WINHTTP_ACCESS_TYPE_NO_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
if(NULL == hSession)
{
dwErr = ::GetLastError();
}
else
{
///
hConnect = ::WinHttpConnect(hSession,pswzServerName,INTERNET_DEFAULT_HTTPS_PORT,0);
if(NULL == hConnect)
{
dwErr = ::GetLastError();
}
else
{
// Use WINHTTP_FLAG_SECURE flag to verify CRL
hRequest = ::WinHttpOpenRequest(hConnect,
NULL,
pswzObjectName,
0,
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
if(NULL == hRequest)
{
dwErr = ::GetLastError();
}
else
{
DWORD dwOpt = WINHTTP_ENABLE_SSL_REVOCATION;
const BOOL bSetOptionResults = ::WinHttpSetOption(hRequest,
WINHTTP_OPTION_ENABLE_FEATURE,
&dwOpt,
sizeof(dwOpt));
if(!bSetOptionResults)
{
dwErr = ::GetLastError();
}
else
{
callback_param_t param;
param.hInet = hRequest;
param.dwErrCert = 0;
const WINHTTP_STATUS_CALLBACK isCallback= ::WinHttpSetStatusCallback(hRequest,SyncCallback,WINHTTP_CALLBACK_FLAG_SECURE_FAILURE,0);
if(WINHTTP_INVALID_STATUS_CALLBACK == isCallback)
{
dwErr = ::GetLastError();
}
else
{
const BOOL bSendResults = ::WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,reinterpret_cast<DWORD_PTR>(¶m));
if(!bSendResults)
{
dwErr = ::GetLastError();
// Value is set to lpdwErrCert, if an error occurred in CRL check.
if(lpdwErrCert)
{
*lpdwErrCert = param.dwErrCert;
}
}
else
{
// Place additional code here.
// For instance, receive response
}
}
}
}
::WinHttpCloseHandle(hConnect);
}
::WinHttpCloseHandle(hSession);
}
return dwErr;
}
static VOID CALLBACK SyncCallback(HINTERNET inet,
DWORD_PTR context,
DWORD status,
PVOID information,
DWORD informationLength)
{
callback_param_t &p = *reinterpret_cast<callback_param_t*>(context);
const DWORD flag = reinterpret_cast<DWORD>(information);
if((0 != context) &&
(inet == p.hInet) &&
(WINHTTP_CALLBACK_STATUS_SECURE_FAILURE == status) &&
(sizeof(DWORD) == informationLength))
{
p.dwErrCert = flag;
}
}
int main(int argc, char **argv)
{
DWORD dwErrCert = 0;
DWORD dwErr = ConnectHTTPSFunc(L”https://10.10.117.183“, L”/”, &dwErrCert);
if((ERROR_SUCCESS != dwErr) && (0 != dwErrCert))
{
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED)
{
puts(“Certification revocation checking has been enabled, “
“but the revocation check failed to verify whether “
“a certificate has been revoked. The server used “
“to check for revocation might be unreachable.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT)
{
puts(“SSL certificate is invalid.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED)
{
puts(“SSL certificate was revoked.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA)
{
puts(“The function is unfamiliar with the Certificate “
“Authority that generated the server’s certificate.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID)
{
puts(“SSL certificate common name (host name field) “
“is incorrect, for example, if you entered “
“www.microsoft.com and the common name on the “
“certificate says www.msn.com.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID)
{
puts(“SSL certificate date that was received from the “
“server is bad. The certificate is expired.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR)
{
puts(“The application experienced an internal error “
“loading the SSL libraries.”);
}
if(dwErrCert & WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE)
{
puts(“WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE”);
}
}
return dwErr;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157664.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...