http客户端发送消息
dingyaoyang opened this issue · comments
dingyaoyang commented
客户端发送data
WFFacilities::WaitGroup wait_group(1);
WFHttpTask *task = WFTaskFactory::create_http_task(uuu, REDIRECT_MAX, RETRY_MAX,
[&res,&state,&wait_group](WFHttpTask *task)
{
state = task->get_state();
auto *resp = task->get_resp();
if (state == WFT_STATE_SUCCESS)
{
const void *body = nullptr;
size_t size = 0;
resp->get_parsed_body(&body, &size);
res = std::string((char *)body, size);
}
wait_group.done();
}
);
task->get_req()->append_output_body(data);
task->start();
wait_group.wait();
服务拿消息:
WFHttpServer server([](WFHttpTask* task){
printf("%d\n",task->get_state());
printf("%d\n",task->get_error());
const void * body;
size_t size;
task->get_resp()->get_parsed_body(&body, &size);
printf("Get%d: %s\n", size, body);
});
server.start(9990);
while(1){}
返回:
3
0
Get-1126863808: �y��
为什么拿不到消息
xiehan commented
你能把client的代码贴完整吗?
xiehan commented
是不是你二进制数据的原因?printf打印的是字符串,不能打印二进制吧。我wget里的示例,是用fwrite输出的。
dingyaoyang commented
bool BSConnect::uploadLog(BSLDATA data)
{
json jstr;
jstr["businessType"] = data.businessType;
jstr["businessName"] = UrlEncode(data.businessName);
jstr["requestData"] = UrlEncode(data.requestData);
jstr["opResult"] = data.opResult;
jstr["opReason"] = UrlEncode(data.opReason);
jstr["useTime"] = data.useTime;
jstr["requestTime"] = data.requestTime;
std::string res = CreateTask(uploadUri, jstr.dump());
// 结果判断
if(res.empty())
{
return false;
}
// 解析json
json jres;
try
{
jres = json::parse(res);
}
catch(const json::exception& e)
{
return false;
}
if(jres["retCode"] == 1)
{
return true;
}
else
{
return false;
}
}
std::string BSConnect::CreateTask(std::string uri, std::string data)
{
std::string uuu = url+uri;
if(uri == uploadUri)
{
uuu = "http://127.0.0.1:9990";
}
std::string res;
int state;
WFFacilities::WaitGroup wait_group(1);
WFHttpTask *task = WFTaskFactory::create_http_task(uuu, REDIRECT_MAX, RETRY_MAX,
[&res,&state,&wait_group](WFHttpTask *task)
{
state = task->get_state();
auto *resp = task->get_resp();
if (state == WFT_STATE_SUCCESS)
{
const void *body = nullptr;
size_t size = 0;
resp->get_parsed_body(&body, &size);
res = std::string((char *)body, size);
size = 123;
}
wait_group.done();
}
);
task->get_req()->append_output_body(data);
task->start();
wait_group.wait();
return res;
}
传的数据"{"businessName":"SDF_OpenSession","businessType":1,"opReason":"test","opResult":1,"requestData":"%7B%22hDeviceHadnle%22%3A1%7D","requestTime":"2024-04-16 19:15:10","useTime":"35"}"
dingyaoyang commented
数据长度也没拿到
xiehan commented
server能运行到process,但get_parsed_body()却拿不到你的请求和数据长度?size得到是的多少?
xiehan commented
应该是task->get_req()->get_parsed_body() .... 这是request啊,不是response。
dingyaoyang commented
还有一个问题,我用workflow传数据,java后台(也是http协议,没用workflw)能拿到吗?
xiehan commented
肯定可以啊,要不然协议有什么用。
dingyaoyang commented
好的,谢谢