#include "stdafx.h" #include "HttpClient.h" #include "Agv.h" #include "curl.h" char getResponseStr[10240] = { 0 }; //回调函数 size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) { TRACE("%s\r\n", (char*)ptr); //string data((const char*) ptr, (size_t) size * nmemb); memcpy((char*)stream+strlen((char*)stream), ptr, size*nmemb); //*((stringstream*) stream) << data << endl; return size * nmemb; } CHttpClient::CHttpClient() { char acIp[64] = { 0 }; GetPrivateProfileString("WMS-SERVER", "IP", "0.0.0.0", acIp, 63, theApp.m_strConfigFile); m_strIp = acIp; char acUrl[128] = { 0 }; GetPrivateProfileString("WMS-SERVER", "WCS", "0.0.0.0", acUrl, 128, theApp.m_strConfigFile); m_strUrl = acUrl; m_nPort = (short)GetPrivateProfileInt("WMS-SERVER", "PORT", 0, theApp.m_strConfigFile); //LogOutToFile("[info] 连接服务器:[%s][%s:%d]", m_strWmsUrl.GetBuffer(), acIp, m_nPort); } CHttpClient::~CHttpClient(void) { } string get_string(string res) { //删除换行符 int r = res.find('\r\n'); while (r != string::npos) { if (r != string::npos) { res.replace(r, 1, ""); r = res.find('\r\n'); } } return res; } CString CHttpClient::WmsCallBack(CString strSender, CString strMsgType, CString strParam) { LogOutToFile("CHttpClient::WmsCallBack Start"); CString strUrl = "/api/Wcs/RecvMsgFromWcs"; INTERNET_PORT m_Port = m_nPort; CString strHost = m_strIp; try { CInternetSession m_Session("HttpClient"); CHttpConnection * m_Server = NULL; m_Server = m_Session.GetHttpConnection(strHost, m_Port); CHttpFile*pFile = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST, strUrl); CString strHeaders, tempStr; CString strHeader = _T("Content-Type: application/x-www-form-urlencoded\r\n"); //pFile->AddRequestHeaders("Accept: application / json"); //pFile->AddRequestHeaders("Content-Type:application/json"); //pFile->AddRequestHeaders("Content-Type:charset=UTF-8"); Json::Value root; root["sender"] = strSender.GetBuffer(); root["receiver"] = "WMS"; TRACE("回复消息%s\r\n",strMsgType); //消息类型转换 if (strMsgType == "AGV_ENTER_INTO_ROBOT_REQ") //AGV请求进去机器人作业区域 { strMsgType = "AGV_RESUME";//AGV恢复运行 } else if (strMsgType == "FORKLIFT_ENTER_INTO_ROBOT_REQ")//FORKLIFT请求进入机器人作业区域 { strMsgType = "FORKLIFT_RESUME"; //叉车恢复运行 } else if (strMsgType == "FORKLIFT_ENTER_INTO_STACKER_REQ")//FORKLIFT请求进入码垛机 { strMsgType = "FORKLIFT_RESUME"; //叉车恢复运行 } /*else if (strMsgType == "FORKLIFT_LEAVE_FROM_ROBOT")//FORKLIFT从码垛机退出 { strMsgType = "FORKLIFT_RESUME"; //叉车恢复运行 }*/ /* else if (strMsgType == "AGV_END") strMsgType = "AGV_LEAVE"; else if (strMsgType == "FORKLIFT_PALLET_PAUSE") strMsgType = "FORKLIFT_PALLET_RESUME"; else if (strMsgType == "FORKLIFT_PALLET_END") strMsgType = "FORKLIFT_PALLET_LEAVE"; else if (strMsgType == "FORKLIFT_GET_END") strMsgType = "FORKLIFT_GET_LEAVE"; */ root["type"] = strMsgType.GetBuffer(); root["params"] = strParam.GetBuffer(); Json::FastWriter writer; string strJson1 = "params=" + writer.write(root); //string strJson1 = writer.write(root); //AfxMessageBox(strJson1.c_str()); pFile->SendRequest(strHeader, (char*)strJson1.c_str(), strJson1.length()); DWORD httpStatus; //获得请求的状态 pFile->QueryInfoStatusCode(httpStatus); CString statusCode; CString strResult; if (httpStatus == HTTP_STATUS_OK) { CString strLine; int m_read; while ((m_read = pFile->ReadString(strLine)) > 0) strResult += strLine; } else { //AfxMessageBox("HTTP_STATUS_FAILED\n"); //LogOutToFile("[info] 仓储状态上传返回状态...%d", httpStatus); } if (pFile) delete pFile; if (m_Server) delete m_Server; LogOutToFile("CHttpClient::WmsCallBack End1"); m_Session.Close(); //AfxMessageBox(strResult); return strResult; //AfxMessageBox(strResult); } catch (...) { //AfxMessageBox("error2"); } LogOutToFile("CHttpClient::WmsCallBack End2"); return ""; } CString CHttpClient::FastCallBack(CString strUrl, CString strParam) { //CString strUrl = "/api/project.benma.Fast/CallbacakFromWcs"; INTERNET_PORT m_Port = m_nPort; CString strHost = m_strIp; LogOutToFile("CHttpClient::FastCallBack Start"); try { CInternetSession m_Session("HttpClient"); CHttpConnection * m_Server = NULL; m_Server = m_Session.GetHttpConnection(strHost, m_Port); CHttpFile*pFile = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST, strUrl); CString strHeaders, tempStr; CString strHeader = _T("Content-Type: application/x-www-form-urlencoded\r\n"); //pFile->AddRequestHeaders("Accept: application / json"); //pFile->AddRequestHeaders("Content-Type:application/json"); //pFile->AddRequestHeaders("Content-Type:charset=UTF-8"); Json::Value root; root["sender"] = "TEST"; root["receiver"] = "WMS"; root["params"] = strParam.GetBuffer(); Json::FastWriter writer; string strJson1 = "params=" + writer.write(root); //string strJson1 = writer.write(root); //AfxMessageBox(strJson1.c_str()); pFile->SendRequest(strHeader, (char*)strJson1.c_str(), strJson1.length()); DWORD httpStatus; //获得请求的状态 pFile->QueryInfoStatusCode(httpStatus); CString statusCode; CString strResult; if (httpStatus == HTTP_STATUS_OK) { CString strLine; int m_read; while ((m_read = pFile->ReadString(strLine)) > 0) strResult += strLine; } else { //AfxMessageBox("HTTP_STATUS_FAILED\n"); //LogOutToFile("[info] 仓储状态上传返回状态...%d", httpStatus); } if (pFile) delete pFile; if (m_Server) delete m_Server; m_Session.Close(); //AfxMessageBox(strResult); LogOutToFile("CHttpClient::FastCallBack End1"); return strResult; //AfxMessageBox(strResult); } catch (...) { //AfxMessageBox("error2"); } LogOutToFile("CHttpClient::FastCallBack End2"); return ""; } CString CHttpClient::AbnormalCallBack(CString strType, CString strParam) { CString strUrl = "/api/Runlog/AbnormalCallBack"; INTERNET_PORT m_Port = m_nPort; CString strHost = m_strIp; LogOutToFile("CHttpClient::AbnormalCallBack Start"); try { CInternetSession m_Session("HttpClient"); CHttpConnection * m_Server = NULL; m_Server = m_Session.GetHttpConnection(strHost, m_Port); CHttpFile*pFile = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST, strUrl); CString strHeaders, tempStr; CString strHeader = _T("Content-Type: application/x-www-form-urlencoded\r\n"); //pFile->AddRequestHeaders("Accept: application / json"); //pFile->AddRequestHeaders("Content-Type:application/json"); //pFile->AddRequestHeaders("Content-Type:charset=UTF-8"); Json::Value root; root["type"] = _UnicodeToUtf8(strType); root["content"] = _UnicodeToUtf8(strParam); //root["params"] = strParam.GetBuffer(); Json::FastWriter writer; string strJson1 = "params=" + writer.write(root); //string strJson1 = writer.write(root); //AfxMessageBox(strJson1.c_str()); pFile->SendRequest(strHeader, (char*)strJson1.c_str(), strJson1.length()); DWORD httpStatus; //获得请求的状态 pFile->QueryInfoStatusCode(httpStatus); CString statusCode; CString strResult; if (httpStatus == HTTP_STATUS_OK) { CString strLine; int m_read; while ((m_read = pFile->ReadString(strLine)) > 0) strResult += strLine; } else { //AfxMessageBox("HTTP_STATUS_FAILED\n"); //LogOutToFile("[info] 仓储状态上传返回状态...%d", httpStatus); } if (pFile) delete pFile; if (m_Server) delete m_Server; m_Session.Close(); //AfxMessageBox(strResult); LogOutToFile("CHttpClient::AbnormalCallBack End1"); return strResult; //AfxMessageBox(strResult); } catch (...) { //AfxMessageBox("error2"); } LogOutToFile("CHttpClient::AbnormalCallBack End2"); return ""; } CString CHttpClient::GenericPost(CString strParam) { LogOutToFile("CHttpClient::GenericPost Start"); INTERNET_PORT m_Port = m_nPort; CString strHost = m_strIp; //CString strRequrl; //strRequrl.Format("%s:%d%s", m_strIp, m_nPort, m_strUrl); //AfxMessageBox(strRequrl); try { CInternetSession m_Session("HttpClient"); CHttpConnection * m_Server = NULL; m_Server = m_Session.GetHttpConnection(strHost, m_Port); CHttpFile*pFile = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST, m_strUrl); //AfxMessageBox(m_strUrl); CString strHeaders, tempStr; CString strHeader = _T("Content-Type: application/x-www-form-urlencoded\r\n"); //pFile->AddRequestHeaders("Accept: application / json"); //pFile->AddRequestHeaders("Content-Type:application/json"); //pFile->AddRequestHeaders("Content-Type:charset=UTF-8"); //string strJson1 = writer.write(root); //AfxMessageBox(strJson1.c_str()); pFile->SendRequest(strHeader, (char*)strParam.GetBuffer(), strParam.GetLength()); DWORD httpStatus; //获得请求的状态 pFile->QueryInfoStatusCode(httpStatus); CString statusCode; CString strResult; if (httpStatus == HTTP_STATUS_OK) { CString strLine; int m_read; while ((m_read = pFile->ReadString(strLine)) > 0) strResult += strLine; } else { //AfxMessageBox("HTTP_STATUS_FAILED\n"); //LogOutToFile("[info] 仓储状态上传返回状态...%d", httpStatus); } //AfxMessageBox(strResult); if (pFile) delete pFile; if (m_Server) delete m_Server; LogOutToFile("CHttpClient::WmsCallBack End1"); m_Session.Close(); //AfxMessageBox(strResult); return strResult; //AfxMessageBox(strResult); } catch (...) { //AfxMessageBox("error2"); } LogOutToFile("CHttpClient::WmsCallBack End2"); return ""; }