VC实现单向认证SSL连接POST数据源码

  1. #include "StdAfx.h"  
  2. #include <afxinet.h>  
  3.   
  4. CInternetSession *g_ISession;  
  5. CHttpConnection *g_pHttpConn = NULL;  
  6. CHttpFile *g_pHttpFile = NULL;  
  7. const char g_szHeaders[]=_T("Accept: */*\r\nUser-Agent:Jinhill Http Agent\r\nContent-Type: application/x-www-form-urlencoded");  
  8. int g_nHeaderLen = strlen(g_szHeaders);  
  9.   
  10. int  HttpConnect(char *szURL)  
  11. {  
  12.     if(szURL == NULL)  
  13.     {  
  14.         return -1;  
  15.     }  
  16.     CString strServerName = "localhost";  
  17.     CString strObject = "/";  
  18.     INTERNET_PORT nPort = 443;  
  19.     DWORD dwServiceType = 0;  
  20.     DWORD dwReqFlags = 0;  
  21.     BOOL bRV = AfxParseURL(szURL,  
  22.                         dwServiceType,  
  23.                         strServerName,  
  24.                         strObject,  
  25.                         nPort );  
  26.     if (!bRV)   
  27.     {   
  28.         return -2;   
  29.     }  
  30.   
  31.     try  
  32.     {  
  33.         g_ISession = new  CInternetSession();  
  34.         g_pHttpConn = g_ISession->GetHttpConnection(strServerName, INTERNET_FLAG_SECURE | SECURITY_FLAG_IGNORE_UNKNOWN_CA, nPort);   
  35.         g_pHttpFile = g_pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_POST,   
  36.             strObject, NULL, 1, NULL, NULL,INTERNET_FLAG_SECURE);  
  37.         g_pHttpFile->AddRequestHeaders(g_szHeaders);  
  38.         dwReqFlags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID  | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_REVOCATION | SECURITY_FLAG_IGNORE_WRONG_USAGE;  
  39.         g_pHttpFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwReqFlags, sizeof(dwReqFlags));  
  40.     }  
  41.     catch(CInternetException*)  
  42.     {  
  43.         if(g_pHttpFile)  
  44.         {  
  45.             g_pHttpFile->Close();  
  46.             g_pHttpFile = NULL;  
  47.         }  
  48.         if (g_pHttpConn != NULL)   
  49.         {  
  50.             g_pHttpConn->Close();  
  51.             delete g_pHttpConn;  
  52.         }  
  53.         return -3;   
  54.     }  
  55.     return 0;  
  56. }  
  57.   
  58. int   SendData(char *pbData, DWORD dwLen)  
  59. {  
  60.     if(g_pHttpConn == NULL ||  
  61.         g_pHttpFile == NULL ||  
  62.         pbData == NULL ||  
  63.         dwLen <= 0 )  
  64.     {  
  65.         return -1;  
  66.     }  
  67.     try  
  68.     {  
  69.         BOOL bRV = g_pHttpFile->SendRequest(g_szHeaders, g_nHeaderLen, pbData, dwLen);   
  70.         if(!bRV)  
  71.         {  
  72.             return -4;  
  73.         }  
  74.     }  
  75.     catch(CInternetException*)  
  76.     {  
  77.         return -3;  
  78.     }  
  79.     return 0;  
  80. }  
  81.   
  82. int   ReadData(char *pbData, DWORD dwLen)  
  83. {  
  84.     if(g_pHttpConn == NULL ||  
  85.         g_pHttpFile == NULL ||  
  86.         pbData == NULL ||  
  87.         dwLen <= 0 )  
  88.     {  
  89.         return -1;  
  90.     }  
  91.     int nReadLen = 0;  
  92.     try  
  93.     {  
  94.         nReadLen = g_pHttpFile->Read(pbData, dwLen);  
  95.     }  
  96.     catch(CInternetException*)  
  97.     {  
  98.         return -3;  
  99.     }  
  100.     return nReadLen;  
  101. }  
  102.   
  103. void   HttpClose()  
  104. {  
  105.     if(g_pHttpFile)  
  106.     {  
  107.         g_pHttpFile->Close();  
  108.         g_pHttpFile = NULL;  
  109.     }  
  110.     if(g_pHttpConn)  
  111.     {  
  112.         g_pHttpConn->Close();  
  113.         g_pHttpConn = NULL;  
  114.     }  
  115.     if(g_ISession)  
  116.     {  
  117.         g_ISession->Close();  
  118.         delete g_ISession;  
  119.     }  
  120. }  
  121.   
  122. void CTestHttpsDlg::OnOK()   
  123. {  
  124.     int rv = -1;  
  125.     char pbSendData[1024] = "flag=sign&a=1&b=2";  
  126.     int nSendLen = strlen(pbSendData);  
  127.     char pbRecvData[1024] = {0};  
  128.     int nRecvLen = sizeof(pbRecvData);  
  129.     int nLen = 0;  
  130.     rv = HttpConnect("https://10.0.89.12/test.jsp");  
  131.     if( rv != 0)  
  132.     {  
  133.         return;  
  134.     }  
  135.     rv = SendData(pbSendData, nSendLen);  
  136.     if( rv != 0)  
  137.     {  
  138.         return;  
  139.     }  
  140.     while(1)  
  141.     {  
  142.         rv = ReadData(pbRecvData, nRecvLen);  
  143.         if( rv <= 0)  
  144.         {  
  145.             break;  
  146.         }  
  147.         OutputDebugString(pbRecvData);  
  148.     }  
  149.   
  150.     strcpy(pbSendData, "flag=check&a=4&b=5");  
  151.     rv = SendData(pbSendData, nSendLen);  
  152.     if( rv != 0)  
  153.     {  
  154.         return;  
  155.     }  
  156.     while(1)  
  157.     {  
  158.         rv = ReadData(pbRecvData, nRecvLen);  
  159.         if( rv <= 0)  
  160.         {  
  161.             break;  
  162.         }  
  163.         OutputDebugString(pbRecvData);  
  164.     }  
  165.       HttpClose();  
  166. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值