WinHttp应用demo

WinHttp应用demo#include#include#include#pragmacomment(lib,”winhttp”)structcallback_param_t{HINTERNEThInet;DWORDdwErrCert;};staticVOIDCALLBACKSyncCallback(HINTERNET,DWORD_PTR,DWORD,

大家好,又见面了,我是你们的朋友全栈君。

#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>(&param));

                                                        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;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157664.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 比太钱包使用方法及冷钱包存储方案-2

    拖更一个月的我又开始更新了。过完年,恰逢数字货币开始上涨,正好是聊聊钱包的好时候。上文说到比太钱包的冷热模式,这次接上文写冷钱包模式下的发币及公钥监控操作。先从冷钱包模式下发币说起吧:1,打开钱包,点击发送按钮。2,输入收款地址和金额,并点击发送。这里无论是输入比特币金额还是法币金额,钱包会自动换算出另一种货币金额。3,输入钱包密码,准备冷钱包签名。4,生成未签名的交易信息二维码,需使用装有对应…

    2022年4月5日
    152
  • Java面试题–较经典

    Java面试题–较经典1、出处:2016年360Java面试题:原题:首先 代码跑一边 保证正确性。分析:往方法中传参,传的仅仅只是地址,而不是实际内存,所以不要以为y=x程序的执行,是 b=a的执行。这两者是不相等的。 2、出处:2016年 阿里巴巴Java面试题:原题:分析:本题是一个自动拆装箱的考题(自动拆装箱JDK需在1.5上)参考:https://blog….

    2022年6月13日
    31
  • 各种数据库的分页查询语句[通俗易懂]

    各种数据库的分页查询语句

    2022年3月2日
    44
  • pycharm 3.2激活码 2022【在线注册码/序列号/破解码】

    pycharm 3.2激活码 2022【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    63
  • 一款你不容错过的Laravel后台管理扩展包 —— Voyager

    一款你不容错过的Laravel后台管理扩展包 —— Voyager

    2021年10月21日
    42
  • 网页刷流量软件开发中的困惑

    网页刷流量软件开发中的困惑客户要求:1.利用代理IP访问指定网页.并且点击指定位置2.代理IP获取方式最好为吸附.吸附我提供的地址.或许你有更好的方案最佳.3.如果能够控制代理访问量最好.比如我在指定的时间内10点-11点的时候需要1W的量.那么程序刷到1W的量就自动停止.4.可以用协议开发.但是协议一定要支持统计代码.也就是一定要让后台统计到具体数据.我的解决方案有二:。一是模…

    2022年9月29日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号