|
C++实现http请求的代码,参照网上的修改了下在mfc中使用
1、HttpClient.h
-
-
#ifndefHTTPCLIENT_H
-
#defineHTTPCLIENT_H
-
-
#include<afxinet.h>
-
#include<string>
-
usingnamespacestd;
-
-
#defineIE_AGENT_T("Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;.NETCLR2.0.50727)")
-
-
-
#defineSUCCESS0
-
-
#defineFAILURE1
-
-
#defineOUTTIME2
-
-
classCHttpClient
-
{
-
public:
-
CHttpClient(LPCTSTRstrAgent=IE_AGENT);
-
virtual~CHttpClient(void);
-
-
intHttpGet(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
-
intHttpPost(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
-
intHttpPut(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
-
-
private:
-
intExecuteRequest(intstrMethod,LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse);
-
voidClear();
-
-
-
private:
-
CInternetSession*m_pSession;
-
CHttpConnection*m_pConnection;
-
CHttpFile*m_pFile;
-
};
-
-
#endif//HTTPCLIENT_H
2、HttpClient.cpp
-
-
#include"stdafx.h"
-
#include"HttpClient.h"
-
-
#defineBUFFER_SIZE1024
-
-
#defineNORMAL_CONNECTINTERNET_FLAG_KEEP_CONNECTION
-
#defineSECURE_CONNECTNORMAL_CONNECT|INTERNET_FLAG_SECURE
-
#defineNORMAL_REQUESTINTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE
-
#defineSECURE_REQUESTNORMAL_REQUEST|INTERNET_FLAG_SECURE|INTERNET_FLAG_IGNORE_CERT_CN_INVALID
-
-
CHttpClient::CHttpClient(LPCTSTRstrAgent)
-
{
-
m_pSession=newCInternetSession(strAgent);
-
m_pConnection=NULL;
-
m_pFile=NULL;
-
}
-
-
-
CHttpClient::~CHttpClient(void)
-
{
-
Clear();
-
if(NULL!=m_pSession)
-
{
-
m_pSession->Close();
-
deletem_pSession;
-
m_pSession=NULL;
-
}
-
}
-
-
voidCHttpClient::Clear()
-
{
-
if(NULL!=m_pFile)
-
{
-
m_pFile->Close();
-
deletem_pFile;
-
m_pFile=NULL;
-
}
-
-
if(NULL!=m_pConnection)
-
{
-
m_pConnection->Close();
-
deletem_pConnection;
-
m_pConnection=NULL;
-
}
-
}
-
-
intCHttpClient::ExecuteRequest(intstrMethod,LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
-
{
-
intresult=FAILURE;
-
-
CStringstrServer;
-
CStringstrObject;
-
DWORDdwServiceType;
-
INTERNET_PORTnPort;
-
-
-
AfxParseURL(strUrl,dwServiceType,strServer,strObject,nPort);
-
if(AFX_INET_SERVICE_HTTP!=dwServiceType&&AFX_INET_SERVICE_HTTPS!=dwServiceType)
-
{
-
returnFAILURE;
-
}
-
-
try
-
{
-
m_pConnection=m_pSession->GetHttpConnection(strServer,
-
dwServiceType==AFX_INET_SERVICE_HTTP?NORMAL_CONNECT:SECURE_CONNECT,
-
nPort);
-
m_pFile=m_pConnection->OpenRequest(strMethod,strObject,
-
NULL,1,NULL,NULL,
-
(dwServiceType==AFX_INET_SERVICE_HTTP?NORMAL_REQUEST:SECURE_REQUEST));
-
-
-
m_pFile->AddRequestHeaders(L"Accept:*/*,application/json");
-
m_pFile->AddRequestHeaders(L"Accept-Charset:UTF8");
-
m_pFile->AddRequestHeaders(L"Accept-Language:zh-cn;q=0.8,en;q=0.6,ja;q=0.4");
-
m_pFile->AddRequestHeaders(L"Content-Type:application/json");
-
-
-
-
/*请求body内容先转为UTF-8编码,与服务端保持一致,cword为要发送内容*/
-
char*cword;
-
if(strPostData!=NULL){
-
DWORDnum=WideCharToMultiByte(CP_UTF8,0,strPostData,-1,NULL,0,NULL,NULL);
-
cword=(char*)calloc(num,sizeof(char));
-
if(cword==NULL)
-
{
-
free(cword);
-
}
-
memset(cword,0,num*sizeof(char));
-
WideCharToMultiByte(CP_UTF8,0,strPostData,-1,cword,num,NULL,NULL);
-
printf("content长度为%d\n",strlen(cword));
-
m_pFile->SendRequest(NULL,0,cword,strlen(cword));
-
}
-
else{
-
m_pFile->SendRequest(NULL,0,NULL,0);
-
}
-
-
-
DWORDdwRet;
-
m_pFile->QueryInfoStatusCode(dwRet);
-
printf("HTTP_STATUS_code:%d\n",dwRet);
-
if(dwRet==HTTP_STATUS_OK){
-
result=SUCCESS;
-
}
-
-
-
charszChars[BUFFER_SIZE+1]={0};
-
stringstrRawResponse="";
-
UINTnReaded=0;
-
while((nReaded=m_pFile->Read((void*)szChars,BUFFER_SIZE))>0)
-
{
-
szChars[nReaded]='\0';
-
strRawResponse+=szChars;
-
memset(szChars,0,BUFFER_SIZE+1);
-
}
-
-
-
intunicodeLen=MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,NULL,0);
-
WCHAR*pUnicode=newWCHAR[unicodeLen+1];
-
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
-
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1,pUnicode,unicodeLen);
-
strResponse=pUnicode;
-
-
delete[]pUnicode;
-
pUnicode=NULL;
-
-
Clear();
-
}
-
catch(CInternetException*e)
-
{
-
Clear();
-
DWORDdwErrorCode=e->m_dwError;
-
e->Delete();
-
-
DWORDdwError=GetLastError();
-
-
printf("dwError=%d",dwError,0);
-
-
strResponse=L"CInternetException\n";
-
-
if(ERROR_INTERNET_TIMEOUT==dwErrorCode)
-
{
-
returnOUTTIME;
-
}
-
else
-
{
-
returnFAILURE;
-
}
-
}
-
returnresult;
-
}
-
-
intCHttpClient::HttpGet(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
-
{
-
returnExecuteRequest(CHttpConnection::HTTP_VERB_GET,strUrl,NULL,strResponse);
-
}
-
-
intCHttpClient::HttpPost(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
-
{
-
returnExecuteRequest(CHttpConnection::HTTP_VERB_POST,strUrl,strPostData,strResponse);
-
}
-
intCHttpClient::HttpPut(LPCTSTRstrUrl,LPCTSTRstrPostData,CString&strResponse)
-
{
-
returnExecuteRequest(CHttpConnection::HTTP_VERB_PUT,strUrl,strPostData,strResponse);
-
}
|