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)


相关推荐

  • org.apache.lucene.analysis.TokenStream.incrementToken()Z

    org.apache.lucene.analysis.TokenStream.incrementToken()Z在使用lucene3与paoding集成的时候可能会出现以下错误:Exceptioninthread"main"java.lang.AbstractMethodError:org.apache.lucene.analysis.TokenStream.incrementToken()Z atorg.apache.lucene.index.DocInverterPerField.pro…

  • vmware虚拟机怎么设置上网「建议收藏」

    vmware虚拟机怎么设置上网「建议收藏」点击物理主机2-右键点击属性找到ipv43 在设置vmware虚拟机 4-在使用命令ifconfig 查看虚拟机的ip里面含有 inetaddrip 就是现在虚拟机的IP地址5-在使用XShell 链接该ip地址 就可以链接的…

  • 【Custom Mutator Fuzz】Libprotobuf + LibFuzzer联合使用

    【Custom Mutator Fuzz】Libprotobuf + LibFuzzer联合使用终于到了与fuzzer结合使用的章节了,本篇文章为Libprotobufmutatorfuzzinglearning项目的第二个练习,其中有一些坑点,在本文中也进行了标注编写不易,如果能够帮助到你,希望能够点赞收藏加关注哦Thanks♪(・ω・)ノPS:文章末尾有联系方式,交个朋友吧~本文链接:模糊测试系列往期回顾:【CustomMutatorFuzz】简单Protobuf使用练习【CustomMutatorFuzz】ProtocolBuffer基础(下):C++生成代.

  • 手把手教你启用Win10的Linux子系统(超详细)[通俗易懂]

    今天为大家介绍如何才能启用Windows10下的Linux子系统,废话不多说,直接看步骤:启用开发者模式打开设置点击更新和安全点击开发者选项启用开发人员模式更改系统功能使用win+X快捷键调出系统管理菜单后点击应用和功能,然后拉到底下,选择程序和功能选中应用或关闭Windows功能勾选适用于Linux的Windows子系统,然后确认并重启…

  • 两个元素的矩阵乘除法「建议收藏」

    矩阵的乘除法: 1 矩阵相乘,两个矩阵只有当左边的矩阵的列数等于右边矩阵的行数时,两个矩阵才可以进行矩阵的乘法运算 主要方法就是:用左边矩阵的第一行,逐个乘以右边矩阵的列,第一行与第一列各个元素的乘积相加,第一行与第二列的各个元素的乘积相加。。。。第二行也是,逐个乘以右边矩阵的列。。。。第三行。。。。。。。最后得出结果不明白的可以继续往下看   2…

  • 回文字符串判断python实现

    回文字符串判断python实现一:什么是回文字符串例如:abccba,qwerewq等,奇数偶数个都可以;二:实现方法1):使用切片defis_palindromic(num):str_len=len(num)index=str_len//2str1=num[:index]str2=num[index:][::-1]ifstr1==s…

发表回复

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

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