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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CSS3背景渐变属性 linear-gradient(线性渐变)和radial-gradient(径向渐变)建议收藏

    CSS3Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变)。为了更好的应用CSS3Gradient,我们需要先了解一下目前的几种现代浏览器的

    2021年12月20日
    36
  • sublime text2 安装及使用教程

    sublime text2 安装及使用教程1.下载安装包地址:https://www.sublimetext.com/22.安装,一直点下一步就好,将下列选项打钩,这样文件右键就可以直接用sublimetext2打开3.新建一个html

    2022年7月1日
    16
  • 高性能内存池_内存池化

    高性能内存池_内存池化文章目录项目简介内存池的概念定长内存池的实现整体框架设计threadcachethreadcache整体设计threadcache哈希桶映射对齐规则threadcacheTLS无锁访问centralcachecentralcache整体设计centralcache结构设计centralcache核心实现pagecachepagecache整体设计pagecache中获取Span申请内存过程联调threadcache回收内存centralcache回收内存pagecache回收内存释放内存过程联调大于256K

    2022年10月22日
    0
  • 理解条件概率_如何理解条件概率

    理解条件概率_如何理解条件概率版权声明:本文为博主原创文章,未经博主同意不得转载。https://blog.csdn.net/sheismylife/article/details/25009545网上看了一些解释。认为这个比

    2022年8月1日
    5
  • 从零开始学习UCOSII操作系统3–UCOSII启动过程「建议收藏」

    从零开始学习UCOSII操作系统3–UCOSII启动过程「建议收藏」从零开始学习UCOSII操作系统3–UCOSII启动过程1、初始化UCOSII(1)在调用UCOSII在任何的其他的服务之前,UCOSII要求首先调用初始化函数OSInit();这个函数的目的就是在整个系统启动之前,初始化所有的变量和数据结构。(2)其中,在OSInit()函数中建立空闲任务OS_TaskIdle();这个任务总是处于就绪态的,空闲任务的优先级是设置为最低

    2022年6月4日
    26
  • PyCharm使用教程 — 9、PyCharm中的搜索技巧(文件/函数/内容)「建议收藏」

    PyCharm使用教程 — 9、PyCharm中的搜索技巧(文件/函数/内容)「建议收藏」PyCharm搜索(文件、函数、内容)Pycharm对搜索有很强大的支持,非常方便我们在项目中搜索某个关键词,或者函数等等1、文件内检索在文件内Ctrl+F,如下图所示2、文件内替换快捷键Ctrl+R,将搜索到的内容替换成目标内容。说明:保留原有大小写,比如原来的首字母是大写,替换之后仍旧保留首字母大写。如下所示3、项目中查找1、Ctrl+Shift+F该快捷键容易冲突,比如本地如果安装了搜狗输入法,可以先将对应的快捷键关闭再使用。或者通过菜单栏进入,如下图

    2022年8月28日
    1

发表回复

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

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