MFC HTTP get post 极简

 CInternetSession类进行http请求,携带参数+数据包json


CHttpFile *MHTTP::GET(CHttpConnection *connection, CString parameter, DWORD dwServiceType)
{
	// 开启一个HTTP请求
	CHttpFile *pHttpFile = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, parameter,
		NULL, 1, NULL, _T("HTTP/1.1"),
		(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
	if (NULL == pHttpFile) {
		return false;
	}

	// 设置HTTP请求包头
	pHttpFile->AddRequestHeaders(_T("User-Agent: MYPRODUCT/1.0.0 (Windows)"));
	pHttpFile->AddRequestHeaders(_T("Content-Type: application/json"));
	pHttpFile->AddRequestHeaders(_T("Charset: UTF-8"));

	// 发送数据
	BOOL bResult = pHttpFile->SendRequest();
	if (!bResult) {

		CString errText;
		errText.Format(L"发送数据出错,错误码:%d", bResult);
		AfxMessageBox(errText);
		return false;
	}

	return pHttpFile;
}

CHttpFile *MHTTP::POST(CHttpConnection * connection, CString parameter, DWORD dwServiceType)
{
	// 开启一个HTTP请求
	CHttpFile *pHttpFile = connection->OpenRequest(CHttpConnection::HTTP_VERB_POST, parameter,
		NULL, 1, NULL, _T("HTTP/1.1"),
		(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
	if (NULL == pHttpFile) {
		return NULL;
	}

	// 设置HTTP请求包头
	pHttpFile->AddRequestHeaders(_T("User-Agent: MYPRODUCT/1.0.0 (Windows)"));
	pHttpFile->AddRequestHeaders(_T("Content-Type: application/octet-stream"));
	pHttpFile->AddRequestHeaders(_T("Charset: UTF-8"));

	// 发送数据
	BOOL bResult = pHttpFile->SendRequest(NULL, 0, getJson(), strlen(getJson()));
	if (!bResult) {

		CString errText;
		errText.Format(L"发送数据出错,错误码:%d", bResult);
		AfxMessageBox(errText);
		return NULL;
	}

	return pHttpFile;
}

bool MHTTP::Request(CString url, UINT type)
{
	bool error = false;

	CString strServer;
	CString parameter;
	DWORD dwServiceType;
	INTERNET_PORT nPort;

	bool err=AfxParseURL(url, dwServiceType, strServer, parameter, nPort);

	// 打开HTTP连接
	CHttpConnection *pHttpConnection = session.GetHttpConnection(strServer,
		dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
		nPort);
	if (NULL == pHttpConnection)
	{
		goto IS_ERROR;
	}

	CHttpFile *pHttpFile = nullptr;
	switch (type)
	{
	case CHttpConnection::HTTP_VERB_POST:
		pHttpFile = POST(pHttpConnection, parameter, dwServiceType);
		break;
	case CHttpConnection::HTTP_VERB_GET:
		pHttpFile = GET(pHttpConnection, parameter, dwServiceType);
		break;
	}

	// 查询状态
	DWORD dwHttpCode = 0;
	BOOL bResult = pHttpFile->QueryInfoStatusCode(dwHttpCode);
	if (!bResult)
	{
		CString error;
		error.Format(L"查询状态出错,错误码:%d", bResult);
		AfxMessageBox(error);
		goto IS_ERROR1;
	}

	// 出错的原因
	if ((dwHttpCode < 200) || (dwHttpCode >= 300))
	{
		DWORD szBuffer;
		DWORD dwBufferSize = sizeof(szBuffer);
		BOOL bResult = pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, szBuffer, &dwBufferSize);
		CString error;
		error.Format(L"查询状态出错,错误码:%d", szBuffer);
		AfxMessageBox(error);
	}
	else
	{
		UINT nReadBytes;
		char szBuffer[40960] = {};
		memset(szBuffer, 0, 40960);
		// 接收响应
		while ((nReadBytes = pHttpFile->Read((void*)szBuffer, 40960)) > 0)
		{
			_request += szBuffer;
		}
		error = true;
		_dlg_ptr->setText(_request);
	}

IS_ERROR1:
	// 释放资源
	if (NULL != pHttpFile) {
		pHttpFile->Close();
		delete pHttpFile;
		pHttpFile = NULL;
	}

IS_ERROR:
	if (NULL != pHttpConnection) {
		pHttpConnection->Close();
		delete pHttpConnection;
		pHttpConnection = NULL;
	}

	return error;
}

 调用

CString uri = L"http://localhost:80/api/path?key=\"key123\"&unit=\"abc456\"";
char* json("{\"name\": \"开发者\",\"code\" : \"0102\",\"number\" : \"1001\",\"key\" : \"123\"}");
setJson(json);
Request(uri, CHttpConnection::HTTP_VERB_POST);

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bug狂人

让我们和小姐姐唠嗑可以肢愣起来

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值