fast/Platform/HttpClient.cpp

423 lines
9.5 KiB
C++
Raw Normal View History

2025-01-20 10:30:01 +08:00
#include "stdafx.h"
#include "HttpClient.h"
#include "Agv.h"
#include "curl.h"
char getResponseStr[10240] = { 0 };
//<2F>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
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] <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>:[%s][%s:%d]", m_strWmsUrl.GetBuffer(), acIp, m_nPort);
}
CHttpClient::~CHttpClient(void)
{
}
string get_string(string res) {
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>
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("<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>Ϣ%s\r\n",strMsgType);
//<2F><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
if (strMsgType == "AGV_ENTER_INTO_ROBOT_REQ") //AGV<47><56><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
{
strMsgType = "AGV_RESUME";//AGV<47>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
}
else if (strMsgType == "FORKLIFT_ENTER_INTO_ROBOT_REQ")//FORKLIFT<46><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>
{
strMsgType = "FORKLIFT_RESUME"; //<2F><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
}
else if (strMsgType == "FORKLIFT_ENTER_INTO_STACKER_REQ")//FORKLIFT<46><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
strMsgType = "FORKLIFT_RESUME"; //<2F><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
}
/*else if (strMsgType == "FORKLIFT_LEAVE_FROM_ROBOT")//FORKLIFT<46><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>
{
strMsgType = "FORKLIFT_RESUME"; //<2F><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
}*/
/*
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; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
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] <20>ִ<EFBFBD>״̬<D7B4>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>״̬...%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; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
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] <20>ִ<EFBFBD>״̬<D7B4>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>״̬...%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; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
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] <20>ִ<EFBFBD>״̬<D7B4>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>״̬...%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; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
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] <20>ִ<EFBFBD>״̬<D7B4>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>״̬...%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 "";
}