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)
上一篇 2022年7月11日 下午11:00
下一篇 2022年7月11日 下午11:00


相关推荐

  • python如何设置窗口背景色为白色_pycharm怎么将背景颜色设置成白色?「建议收藏」

    python如何设置窗口背景色为白色_pycharm怎么将背景颜色设置成白色?「建议收藏」方法:1、在pycharm中,点击顶部的“文件”选项;2、点击“设置”按钮,进入设置页面;3、点击“编辑器”选项,再点击“颜色&字体”选项;4、点击“控制台的颜色”选项,在右侧的“scheme”菜单中,选择“default”选项,点击确定即可。pycharm背景颜色设置成白色的方法1、如果没有安装pycharm可以先进行安装,安装完成之后我们点击桌面的pycharm图标进入首页。2、进入之…

    2022年8月28日
    6
  • 企业网站制作的决定因素「建议收藏」

    企业网站制作的决定因素「建议收藏」企业在进行网站建站的过程中要需要很多网站相关行的内容,如何建站?我们从哪些方面将网站建设好呢?现在只要懂网页三剑客,有一台电脑、懂点技术就可以做网站了,面对现在参差不齐的建站公司,我们方如何选择,哪些因素是我们应关注的?下面就为大家谈谈几点: 因素一:网站的易用性以前找网络公司,做出的网站管理后台功能简单,导致后期维护、修改和扩展困难,甚至只能付费让制作公司维护,有一些公司制作的网站

    2025年6月19日
    8
  • OleDbCommand更新数据的一些问题

    OleDbCommand更新数据的一些问题using(OleDbConnectionconnection1=newOleDbConnection(connectionString)){OleDbCommandcommand1=newOleDbCommand();command1.Connection=connection1;

    2022年5月12日
    45
  • 一致性hash面试题_java面试算法

    一致性hash面试题_java面试算法为什么要用一致性hash算法?在学习一致性hash算法之前,首先要考虑下为什么要使用它,使用它能解决什么样的问题。带着问题去学习相信理解起来会更容易。大家都知道我们在使用redis分片技术,mycat对数据库进行分库分表时都会面临数据操作规则的问题;比如我们把一条记录存入redis3服务器,那么我们获取的时候如果不指定规则就会根据key在所有的redis服务器中进行遍历查找,显然这种情况是…

    2022年10月5日
    4
  • openclaw和其他平台有什么区别 openclaw优势对比分析【对比】

    openclaw和其他平台有什么区别 openclaw优势对比分析【对比】

    2026年3月13日
    2
  • mysql下载与安装教程_jdk下载与安装教程

    mysql下载与安装教程_jdk下载与安装教程MySQL下载和安装教程下载MySQL数据库可以访问官方网站:https://www.mysql.com/点击DOWNLOADS模块下的Community模块,进行下载或可访问网页:https://dev.mysql.com/downloads/mysql/直接下载MySQL社区服务版进入,选择MySQL的版本及主机目前最新版本是:MySQLCommunityServer8.0.27,点击下载点击后进入下一页面,该页面可注册MySQL账户,可登录MySQL账户,也可不进行注册,直接下

    2025年9月13日
    9

发表回复

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

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