1、定义连接对象(对象在指定.h文件中)
//定义Http请求传输对象
CHttpConnection* m_pConnection;
CHttpFile* m_pFile;
CInternetSession sess;//creat session
2、实现
参数:
LPCTSTR strMethod:请求的方式是POST还是GET;
LPCTSTR strUrl:请求的web地址;
CString strPostData:上传的数据;
CString &strResponse:获取返回值;
CString CWebJNDataOcxCtrl::ExecuteRequest( LPCTSTR strMethod, LPCTSTR strUrl, CString strPostData, CString &strResponse )
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = _T("");
CString strState = _T("");
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort); //解析url
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = _T("");
CString strState = _T("");
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort); //解析url
//判断协议类型是否为http或https
if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
//LoggerCio::notice(LoggerCio::LOG_URL,"*** url 非http或https 协议!");//log输出
return _T("不是http协议");
}
try
{
//CHttpConnection *m_pConnection;//定义在头文件
//获取 CHttpConnection*
m_pConnection = sess.GetHttpConnection(strServer ,nPort);
//获取 CHttpFile*
m_pFile = m_pConnection->OpenRequest(strMethod, strObject,NULL, 1, NULL, NULL,INTERNET_FLAG_EXISTING_CONNECT);
if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
//LoggerCio::notice(LoggerCio::LOG_URL,"*** url 非http或https 协议!");//log输出
return _T("不是http协议");
}
try
{
//CHttpConnection *m_pConnection;//定义在头文件
//获取 CHttpConnection*
m_pConnection = sess.GetHttpConnection(strServer ,nPort);
//获取 CHttpFile*
m_pFile = m_pConnection->OpenRequest(strMethod, strObject,NULL, 1, NULL, NULL,INTERNET_FLAG_EXISTING_CONNECT);
m_pFile -> AddRequestHeaders( _T("Accept:application/json;"));
m_pFile -> AddRequestHeaders( _T("Content-Type:application/json;charset=utf-8;"));
m_pFile -> AddRequestHeaders( _T("Content-Type:multipart/form-data;"));
m_pFile -> AddRequestHeaders( _T("Content-Type:application/json;charset=utf-8;"));
m_pFile -> AddRequestHeaders( _T("Content-Type:multipart/form-data;"));
USES_CONVERSION;
char *pData = T2A(strPostData);
if (NULL == m_pFile)
{
//LOG_FUNC_QUIT_DEBUG(LOG_SYS);
return _T("Open Error!");
}
//发送请求
if(m_pFile->SendRequest(NULL, 0,pData,strlen(pData)))
{
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
CString str = _T("");
while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{//接收返回
szChars[nReaded] = '\0';
strRawResponse += szChars;
str = strRawResponse.c_str();
strResponse = CallBackState(str, strPostData);
//AfxMessageBox(strResponse, MB_ICONINFORMATION);
strState = strResponse;
memset(szChars, 0, BUFFER_SIZE + 1);
}
char *pData = T2A(strPostData);
if (NULL == m_pFile)
{
//LOG_FUNC_QUIT_DEBUG(LOG_SYS);
return _T("Open Error!");
}
//发送请求
if(m_pFile->SendRequest(NULL, 0,pData,strlen(pData)))
{
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
CString str = _T("");
while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{//接收返回
szChars[nReaded] = '\0';
strRawResponse += szChars;
str = strRawResponse.c_str();
strResponse = CallBackState(str, strPostData);
//AfxMessageBox(strResponse, MB_ICONINFORMATION);
strState = strResponse;
memset(szChars, 0, BUFFER_SIZE + 1);
}
//将多字符转宽字节存为 CString
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
CString cs(pUnicode);
delete []pUnicode;
pUnicode = NULL;
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
CString cs(pUnicode);
delete []pUnicode;
pUnicode = NULL;
strResponse = cs;
return strState; //成功
}
else{//请求失败打印错误码
DWORD dwError = GetLastError();
//LoggerCio::notice(LoggerCio::LOG_URL,"*** m_pFile->SendRequest 失败,GetLastError=%d",dwError);
return _T("请求失败"); //错误
}
}
catch (CInternetException* e)
{ //捕获CInternetException异常
DWORD dwErrorCode = e->m_dwError;
e->Delete();
e = NULL;
strState = _T("4");
DWORD dwError = GetLastError();
return strState;
}
}
return strState; //成功
}
else{//请求失败打印错误码
DWORD dwError = GetLastError();
//LoggerCio::notice(LoggerCio::LOG_URL,"*** m_pFile->SendRequest 失败,GetLastError=%d",dwError);
return _T("请求失败"); //错误
}
}
catch (CInternetException* e)
{ //捕获CInternetException异常
DWORD dwErrorCode = e->m_dwError;
e->Delete();
e = NULL;
strState = _T("4");
DWORD dwError = GetLastError();
return strState;
}
}