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;

}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Android studio的gradle教程整理「建议收藏」

    Android studio的gradle教程整理「建议收藏」【Gradle教程】第一章:引言http://ask.android-studio.org/?/article/7【Gradle教程】第二章:概述http://ask.android-studio.org/?/article/6【Gradle教程】第三章:教程http://ask.android-studio.org/?/article/15【Gradle教程】第四章:安装…

  • 深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件「建议收藏」

    深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件「建议收藏」在求解最优化问题中,拉格朗日乘子法(LagrangeMultiplier)和KKT(KarushKuhnTucker)条件是两种最常用的方法。在有等式约束时使用拉格朗日乘子法,在有不等约束时使用KKT条件。  我们这里提到的最优化问题通常是指对于给定的某一函数,求其在指定作用域上的全局最小值(因为最小值与最大值可以很容易转化,即最大值问题可以转化成最小值问题)。提到KKT条件一般会附带的…

  • 高并发下的nginx性能优化实战

    高并发下的nginx性能优化实战

  • redis客户端连接工具连接docker里面redis_gbase客户端连接工具

    redis客户端连接工具连接docker里面redis_gbase客户端连接工具Redis客户端连接工具AnotherRedisDesktopManagermac想用到brew的话,地址:https://www.jianshu.com/p/b7b789a2ed2cAnotherRedisDesktopManager为redis可视化工具,真的巨好用呀!!!原文地址:https://blog.csdn.net/huizhou_achao/article/details/108467792下载及安装教程地址:https://github.com/qishibo/An

  • 字符串的匹配算法_多字符串匹配

    字符串的匹配算法_多字符串匹配目录需求基础知识逻辑解析源码实现需求先简单描述溪源曾经遇到的需求:需求一:项目结果文件中实验结论可能会存在未知类型、转换错误、空指针、超过索引长度等等。这里是类比需求,用日常开发中常出现的错误类型作为需求,如果要以上结论则判断这个项目检测失败;解决方案一:大家常用的方式可能是if(){continue;}esleif(){continue;}…或者switch-case等;方案二:可能会使用集合contain()方法;方案三:依次匹配字符串中字符(暴力匹配);以上两种方案都能解决;然

  • C++中 ostringstream istringstream

    C++中 ostringstream istringstreamC++流都很类似,比如:输入输出流就是cincout从控制台读入写出。字符串流也类似,只是重定向到字符串。istringstream是从字符串读,ostringstream是写到字符串中去,用法跟cincout完全一样。C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含<sstre…

发表回复

您的电子邮箱地址不会被公开。

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