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;
}
MFC发送Post请求
最新推荐文章于 2023-01-12 14:55:42 发布