MFC发送Post请求

std::string string_To_UTF8(const std::string & str)
{
	int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

	wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
	ZeroMemory(pwBuf, nwLen * 2 + 2);

	::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

	int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

	char * pBuf = new char[nLen + 1];
	ZeroMemory(pBuf, nLen + 1);

	::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

	std::string retStr(pBuf);

	delete []pwBuf;
	delete []pBuf;

	pwBuf = NULL;
	pBuf = NULL;

	return retStr;
}

int main()
{
    std::string strReceive;
    CString csReceive =;
    CString url = L"http://192.168.0.110/api/auth/token";
    bool br2 = postHttp(url,string_To_UTF8(strReceive),strlen(strReceive.c_str()),csReceive);
}


bool postHttp(const CString& strUrl,std::string data, int nBytes, CString& strReceive)
{
	BOOL bRes=FALSE;
	DWORD dwServiceType = AFX_INET_SERVICE_HTTP ;	
	CString strServer = L"";   	
	INTERNET_PORT wPort;
	CString strObject = L"";
	if (!AfxParseURL(strUrl, dwServiceType, strServer, strObject, wPort))
	{
		CString info;
		info.Format(_T("%s解析Url失败"),strUrl);
		return false;
	}
	CInternetSession session; // 创建会话
	const int nTimeOut = 1000;
	session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);
	session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 2);
   //session.SetOption(INTERNET_OPTION_USERNAME, dwID);
   //session->SetOption(INTERNET_OPTION_PASSWORD,pStringPWD,lengthPWD);
 
	CHttpConnection* pConn = NULL;
	CHttpFile* pFile = NULL;
	if (AFX_INET_SERVICE_HTTP == dwServiceType)
	{
		pConn = session.GetHttpConnection(strServer, wPort);
		pFile = pConn->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
        //pConn->SetOption(INTERNET_OPTION_PASSWORD,pStringPWD,lengthPWD);
	}
	try
	{ 
        //宽字符问题
        //CString data
		//char* pJson = nullptr;
		//size_t length = 0;
		//const wchar_t* wch=data.AllocSysString();
		//bool br1= CStringHelper::ConvertUTF8(wch,&pJson,&length);
		//LPVOID  pString=(LPVOID)pJson;
		//BOOL bRet = pFile->SendRequest(strHeaders, strHeaders.GetLength(),pString,length);
           
		CString strHeaders( L"Content-Type: application/octet-stream");//请求头
		BOOL bRet = pFile->SendRequest(strHeaders, strHeaders.GetLength(),(LPSTR)(LPCSTR)data.c_str(),data.size());
		if (!bRet)
		{
			bRes =FALSE;
			goto Failure_label;
		}
		bRes =TRUE;
		DWORD dwStatusCode;
		bRet = pFile->QueryInfoStatusCode(dwStatusCode);
		if (HTTP_STATUS_OK != dwStatusCode)//#define HTTP_STATUS_OK  200
		{
			CString strErrInfo;
			pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS, strErrInfo);
			bRes =FALSE;
			CString info;
			info.Format(_T("%s请求错误"),strErrInfo);
			goto Failure_label;
		}

		CString csHEADERS;
		std::vector<std::string>  vecHeaders;
		pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, csHEADERS);
		std::string strHeaders2 = CT2A(csHEADERS.GetBuffer());
		SplitString(strHeaders2,vecHeaders,"\n");
		
		std::string strResponse;
		DWORD dwResponseLength = pFile->GetLength();  
		while (0 != dwResponseLength)  
		{    
			LPSTR szResponse;
			szResponse = new char[dwResponseLength+1];
			szResponse[dwResponseLength] = _T('\0');
			pFile->Read(szResponse, dwResponseLength);  
			strResponse += szResponse;  
			delete szResponse;
			dwResponseLength = pFile->GetLength(); 
		}

		std::string strTemp= UTF8_To_string(strResponse);
		//m_log<<"返回值:"<<strTemp<<"\n";
		bRes =TRUE;
		goto Failure_label;
	}
	catch (CInternetException& e)
	{
		TCHAR szErr[512] = { 0 };
		e.GetErrorMessage(szErr, sizeof(szErr));
		CString strErrInfo;
		pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS, strErrInfo);
		bRes =FALSE;
		goto Failure_label;
	}
	catch (...)
	{
		bRes =FALSE;
		goto Failure_label;
	}

Failure_label:
	if (pConn)
	{
		pConn->Close();
		delete pConn;
		pConn = NULL;
	}
	if (pFile)
	{
		pFile->Close();
		delete pFile;
		pFile = NULL;
	}
	return bRes;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值