- #include "StdAfx.h"
- #include <afxinet.h>
- CInternetSession *g_ISession;
- CHttpConnection *g_pHttpConn = NULL;
- CHttpFile *g_pHttpFile = NULL;
- const char g_szHeaders[]=_T("Accept: */*\r\nUser-Agent:Jinhill Http Agent\r\nContent-Type: application/x-www-form-urlencoded");
- int g_nHeaderLen = strlen(g_szHeaders);
- int HttpConnect(char *szURL)
- {
- if(szURL == NULL)
- {
- return -1;
- }
- CString strServerName = "localhost";
- CString strObject = "/";
- INTERNET_PORT nPort = 443;
- DWORD dwServiceType = 0;
- DWORD dwReqFlags = 0;
- BOOL bRV = AfxParseURL(szURL,
- dwServiceType,
- strServerName,
- strObject,
- nPort );
- if (!bRV)
- {
- return -2;
- }
- try
- {
- g_ISession = new CInternetSession();
- g_pHttpConn = g_ISession->GetHttpConnection(strServerName, INTERNET_FLAG_SECURE | SECURITY_FLAG_IGNORE_UNKNOWN_CA, nPort);
- g_pHttpFile = g_pHttpConn->OpenRequest(CHttpConnection::HTTP_VERB_POST,
- strObject, NULL, 1, NULL, NULL,INTERNET_FLAG_SECURE);
- g_pHttpFile->AddRequestHeaders(g_szHeaders);
- 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;
- g_pHttpFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwReqFlags, sizeof(dwReqFlags));
- }
- catch(CInternetException*)
- {
- if(g_pHttpFile)
- {
- g_pHttpFile->Close();
- g_pHttpFile = NULL;
- }
- if (g_pHttpConn != NULL)
- {
- g_pHttpConn->Close();
- delete g_pHttpConn;
- }
- return -3;
- }
- return 0;
- }
- int SendData(char *pbData, DWORD dwLen)
- {
- if(g_pHttpConn == NULL ||
- g_pHttpFile == NULL ||
- pbData == NULL ||
- dwLen <= 0 )
- {
- return -1;
- }
- try
- {
- BOOL bRV = g_pHttpFile->SendRequest(g_szHeaders, g_nHeaderLen, pbData, dwLen);
- if(!bRV)
- {
- return -4;
- }
- }
- catch(CInternetException*)
- {
- return -3;
- }
- return 0;
- }
- int ReadData(char *pbData, DWORD dwLen)
- {
- if(g_pHttpConn == NULL ||
- g_pHttpFile == NULL ||
- pbData == NULL ||
- dwLen <= 0 )
- {
- return -1;
- }
- int nReadLen = 0;
- try
- {
- nReadLen = g_pHttpFile->Read(pbData, dwLen);
- }
- catch(CInternetException*)
- {
- return -3;
- }
- return nReadLen;
- }
- void HttpClose()
- {
- if(g_pHttpFile)
- {
- g_pHttpFile->Close();
- g_pHttpFile = NULL;
- }
- if(g_pHttpConn)
- {
- g_pHttpConn->Close();
- g_pHttpConn = NULL;
- }
- if(g_ISession)
- {
- g_ISession->Close();
- delete g_ISession;
- }
- }
- void CTestHttpsDlg::OnOK()
- {
- int rv = -1;
- char pbSendData[1024] = "flag=sign&a=1&b=2";
- int nSendLen = strlen(pbSendData);
- char pbRecvData[1024] = {0};
- int nRecvLen = sizeof(pbRecvData);
- int nLen = 0;
- rv = HttpConnect("https://10.0.89.12/test.jsp");
- if( rv != 0)
- {
- return;
- }
- rv = SendData(pbSendData, nSendLen);
- if( rv != 0)
- {
- return;
- }
- while(1)
- {
- rv = ReadData(pbRecvData, nRecvLen);
- if( rv <= 0)
- {
- break;
- }
- OutputDebugString(pbRecvData);
- }
- strcpy(pbSendData, "flag=check&a=4&b=5");
- rv = SendData(pbSendData, nSendLen);
- if( rv != 0)
- {
- return;
- }
- while(1)
- {
- rv = ReadData(pbRecvData, nRecvLen);
- if( rv <= 0)
- {
- break;
- }
- OutputDebugString(pbRecvData);
- }
- HttpClose();
- }
VC实现单向认证SSL连接POST数据源码
最新推荐文章于 2023-04-27 08:53:39 发布