| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- #include "stdafx.h"
- #include "CellHttp.h"
- #include "IvrFlow.h"
- #include "FlowDataProvider.h"
- #include "wininet.h"
- #include "afxinet.h"
- #include "json.h"
- CCellHttp::CCellHttp()
- {
- m_IsSaveRs = 0;
- m_SuccessPos = 0;
- m_FailPos = 0;
- m_Url = "";
- }
- CCellHttp::CCellHttp(CCellHttp & cellHttp) : CCellBase(cellHttp)
- {
- m_IsSaveRs = cellHttp.m_IsSaveRs;
- m_SuccessPos = cellHttp.m_SuccessPos;
- m_FailPos = cellHttp.m_FailPos;
- m_Url = cellHttp.m_Url;
- m_RespVar = cellHttp.m_RespVar;
-
- }
- CCellHttp::~CCellHttp(void)
- {
- }
- /*****************************************************************
- **【函数名称】 copy
- **【函数功能】 拷贝自身
- **【参数】
- **【返回值】 拷贝副本
- ****************************************************************/
- CCellBase * CCellHttp::copy(void)
- {
- CCellBase *pCellBase = new CCellHttp(*this);
- return pCellBase;
- }
- /*****************************************************************
- **【函数名称】 fillData
- **【函数功能】 节点解析,填充数据
- **【参数】 Provider:数据提供器
- **【返回值】 成功true,失败false
- ****************************************************************/
- bool CCellHttp::fillData(IFlowDataProvider& Provider)
- {
- CString Data;
- do
- {
- if (!Provider.getData(CELL_ATTRIBUTE_POS, Data))
- {
- Data = _T("节点号");
- break;
- }
- else
- {
- sscanf_s(Data, _T("%d"), &m_Pos);
- if (m_Pos < 1)
- {
- Data = _T("节点号");
- break;
- }
- }
- if (!Provider.getData(CELL_ATTRIBUTE_HTTP_RESP_VAR, m_RespVar))
- {
- Data = _T("HTTP响应存储变量");
- break;
- }
- if (!Provider.getData(CELL_ATTRIBUTE_URL_STR, m_Url))
- {
- Data = _T("SQL语句");
- break;
- }
- if (!Provider.getData(CELL_ATTRIBUTE_SAVE_FLAG, Data))
- {
- Data = _T("保存标志");
- break;
- }
- else
- {
- if (Data == DATA_BOOL_YES)
- m_IsSaveRs = true;
- else
- m_IsSaveRs = false;
- }
- if (!Provider.getData(CELL_ATTRIBUTE_SUCCESS_POS, Data))
- {
- Data = _T("成功跳转节点");
- break;
- }
- else
- {
- sscanf_s(Data, _T("%d"), &m_SuccessPos);
- if (m_SuccessPos < 0)
- {
- Data = _T("成功跳转节点");
- break;
- }
- }
- if (!Provider.getData(CELL_ATTRIBUTE_FAIL_POS, Data))
- {
- Data = _T("失败跳转节点");
- break;
- }
- else
- {
- sscanf_s(Data, _T("%d"), &m_FailPos);
- if (m_FailPos < 0)
- {
- Data = _T("失败跳转节点");
- break;
- }
- }
- Provider.getData(CELL_ATTRIBUTE_NOTE, m_Note);
- return true;
- } while (false);
- ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{Cell}: 节点[%s]解析失败, '%s'错误"), CELL_NAME_SQL, Data);
- return false;
- }
- /*****************************************************************
- **【函数名称】 Operate
- **【函数功能】 节点执行函数
- **【参数】
- **【返回值】 下一个节点编号
- ****************************************************************/
- int CCellHttp::operate(void)
- {
- if (m_pIvrFlow == NULL)
- return CELL_OP_ERROR;
- CString Info;
- _getCellInfo(Info);
- ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{Cell}: 开始执行[%s]"), Info);
- CString t_Url;
- // 替换SQL语句中的变量
- if (!m_pIvrFlow->replaceVar(m_Url, t_Url))
- {
- ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_WARNING, _T("{Cell}: 执行[%s]出错, 替换URL语句中变量失败, URL = %s"), Info, t_Url);
- return m_FailPos;
- }
-
- CString t_resp;
- t_resp = doGet(t_Url);
- if (t_resp.GetLength() > 0) {
- //response cache
- CString strValue = _T("");
- CString strVarName = _T("RespVar");
- //m_RespVar
- m_pIvrFlow->addVar(m_RespVar, strValue);
- //m_pIvrFlow->findVarValue(m_RespVar, strValue);
- Json::Reader reader;
- Json::Value root;
- std::string strInput;
- strInput = t_resp.GetString();
- if (reader.parse(strInput, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
- {
- std::string TypeStr = root["Type"].asString(); // 访问节点,upload_id = "UP000000"
- if (0 == strcmp(TypeStr.c_str(), "Login"))
- {
- std::string AgentIDStr = root["AgentID"].asString();
- }
- }
- return m_SuccessPos;
- }
-
- // 返回执行结果
- return CELL_OP_ERROR;
- }
- // 以http Get方式请求URL
- CString CCellHttp::doGet(CString href)
- {
- CString httpsource = "";
- CInternetSession session(NULL, 0);
- CHttpFile* pFile = NULL;
- try {
- pFile = (CHttpFile*)session.OpenURL(href);
- }
- catch (CInternetException *e)
- {
- pFile = NULL;
- }
- if (pFile)
- {
- DWORD dwRet;
- pFile->QueryInfoStatusCode(dwRet);
- ILogger::getInstance().log(LOG_CLASS_SOCKET, LOG_LEVEL_NORMAL, "http_status[%d]", dwRet);
- if (dwRet == HTTP_STATUS_OK) {
- CString t_text;
- for (int i = 0; pFile->ReadString(t_text); i++)
- {
- httpsource = httpsource + t_text + "\r\n";
- }
- }
- else
- {
- ILogger::getInstance().log(LOG_CLASS_SOCKET, LOG_LEVEL_WARNING, "http_error,status[%d]", dwRet);
- }
- pFile->Close();
- delete pFile;
- }
- session.Close();
- if (httpsource.GetLength() > 0) {
- CString t_ansi_str = "";
- t_ansi_str = Convert(httpsource, CP_UTF8, CP_ACP);
- return t_ansi_str;
- }
- return httpsource;
- }
- CString CCellHttp::Convert(CString& str, int sourceCodePage, int targetCodePage)
- {
- int iUnicodeLen = MultiByteToWideChar(sourceCodePage, 0, (LPCSTR)str.GetBuffer(), -1, NULL, 0);
- wchar_t *pUnicode = NULL;
- pUnicode = new wchar_t[iUnicodeLen + 1];
- memset(pUnicode, 0, (iUnicodeLen + 1) * sizeof(wchar_t));
- MultiByteToWideChar(sourceCodePage, 0, (LPCSTR)str.GetBuffer(), -1, (LPWSTR)pUnicode, iUnicodeLen);
- BYTE *pTargetData = NULL;
- int iTargetLen = WideCharToMultiByte(targetCodePage, 0, (LPWSTR)pUnicode, -1, (char*)pTargetData, 0, NULL, NULL);
- pTargetData = new BYTE[iTargetLen + 1];
- memset(pTargetData, 0, iTargetLen + 1);
- WideCharToMultiByte(targetCodePage, 0, (LPWSTR)pUnicode, -1, (char*)pTargetData, iTargetLen, NULL, NULL);
- CString strFormatTargetData;
- strFormatTargetData.Format(_T("%s"), pTargetData);
- delete[] pTargetData;
- delete[] pUnicode;
- pTargetData = NULL;
- pUnicode = NULL;
- return strFormatTargetData;
- }
|