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

39 篇文章 0 订阅
(作者:陈波,2012-03-05,转载请注明 Form:http://blog.csdn.net/jinhill/article/details/7321515)
#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();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值