MFC发送POST请求带账号密码验证

方法一:
调用MFC接口,只支持INT格式账号密码

	DWORD dwServiceType = AFX_INET_SERVICE_HTTP ;	
	CString strServer = L"";   	
	INTERNET_PORT wPort ;
	CString strObject = L"";
	BOOL bRes = false;

	if (!AfxParseURL(strUrl, dwServiceType, strServer, strObject, wPort))
	{
		return bRes;
	}
	CInternetSession session; // 创建会话
	const int nTimeOut = 1000;
	session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
	session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 2);
	//这个接口只支持DWORD格式,测试可用
	session.SetOption(INTERNET_OPTION_USERNAME,00002735);
	session.SetOption(INTERNET_OPTION_PASSWORD,654321);

方法二:
对于一些固定账号密码可用PostMan生成加密后的密钥,直接放在请求头中即可。
PostMan在这里插入图片描述

		CString strHeaders =  L"Authorization: Basic MDAwMDI3MzU6NjU0MzIx";
		strHeaders += _T("\r\n");
		strHeaders +=  _T("Content-Type:  text/xml\r\n");

		pFile->AddRequestHeaders(strHeaders) ;
		BOOL bRet = pFile->SendRequest(strHeaders, strHeaders.GetLength(),(LPSTR)(LPCSTR)data.c_str(),data.size());

方法二改进版:
支持任意类型动态密码,用base64加密工具(也就是Postman的功能)生成密钥。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static const char b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char reverse_table[128] =
{
	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
	64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
	64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
	64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
};

unsigned char *base64_encode(unsigned char *bindata,size_t inlen,unsigned char **out,size_t *outlen)
{
	size_t _outlen = *outlen;
	unsigned char *_out = NULL;
	size_t out_pos = 0;

	if(NULL == *out)
	{
		_outlen = (inlen / 3 + (inlen%3 != 0)) * 4 + 1;
		_out = (unsigned char *)malloc(_outlen);
	}
	else
	{
		_outlen = *outlen;
		_out = *out;
	}

	memset(_out,'=',_outlen);
	_out[_outlen-1] = 0;

	unsigned int bits_collected = 0;
	unsigned int accumulator = 0;
	for(int i = 0; i < inlen; i++)
	{
		accumulator = (accumulator << 8) | (bindata[i] & 0xffu);
		bits_collected += 8;
		while (bits_collected >= 6)
		{
			bits_collected -= 6;
			_out[out_pos++] = b64_table[(accumulator >> bits_collected) & 0x3fu];
		}
	}

	if(bits_collected >= 6)
	{
		if(NULL == *out)
		{
			free(_out);
		}
		return NULL;
	}

	if (bits_collected > 0)
	{
		// Any trailing bits that are missing.
		accumulator <<= 6 - bits_collected;
		_out[out_pos++] = b64_table[accumulator & 0x3fu];
	}

	*outlen = _outlen;
	*out = _out;
	return _out;
}

unsigned char *base64_decode(unsigned char *bindata,size_t inlen,unsigned char **out,size_t *outlen)
{
	size_t _outlen = *outlen;
	unsigned char *_out = NULL;
	int bits_collected = 0;
	unsigned int accumulator = 0;
	size_t out_pos = 0;

	if(NULL == *out)
	{
		_outlen = inlen;
		_out = (unsigned char *)malloc(_outlen);
	}
	else
	{
		_outlen = *outlen;
		_out = *out;
	}

	int c = 0;
	for(int i = 0; i < inlen; i++)
	{
		c = bindata[i];
		if (isspace(c) || c == '=')
		{
			// Skip whitespace and padding. Be liberal in what you accept.
			continue;
		}
		if ((c > 127) || (c < 0) || (reverse_table[c] > 63))
		{
			return NULL;
		}
		accumulator = (accumulator << 6) | reverse_table[c];
		bits_collected += 6;
		if (bits_collected >= 8)
		{
			bits_collected -= 8;
			_out[out_pos++] = (char)((accumulator >> bits_collected) & 0xffu);
		}
	}

	*outlen = _outlen;
	*out = _out;
	return _out;
}
	//调用方法
 	 unsigned char* strcode = (unsigned char*)"00002735:654321";//账户:密码
	 int sLen = strlen((char*)strcode);

	 unsigned char *out = 0;
	 size_t len = 0;
	 unsigned char* encode =  base64_encode(strcode,strlen((char*)strcode),&out,&len);

运行结果图

方法三:
还有一个MFC提供的任意类型账号密码接口但调用失败(使用姿势不对)。有会用的麻烦指正下哪里有问题,多谢

	if (!AfxParseURL(strUrl, dwServiceType, strServer, strObject, wPort))
	{
		return bRes;
	}
	CInternetSession session; // 创建会话
	const int nTimeOut = 1000;
	session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
	session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 2);
	
	char* pID = nullptr;
	size_t lengthID = 0;
	const wchar_t* wchID=m_mapCfg[L"ID"].AllocSysString();
	bool brID= CStringHelper::ConvertUTF8(wchID,&pID,&lengthID);
	LPVOID  pStringID=(LPVOID)pID;
	session.SetOption(INTERNET_OPTION_USERNAME,pStringID,lengthID);

	char* pPWD = nullptr;
	size_t lengthPWD = 0;
	const wchar_t* wchPWD=m_mapCfg[L"PWD"].AllocSysString();
	bool brPWD= CStringHelper::ConvertUTF8(wchPWD,&pPWD,&lengthPWD);
	LPVOID  pStringPWD=(LPVOID)pPWD;
	session.SetOption(INTERNET_OPTION_PASSWORD,pStringPWD,lengthPWD);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值