[英]Poco HTTPClientSession adding headers to HTTPRequest
Try to send POST method to server using JSON. 尝试使用JSON将POST方法发送到服务器。 But, server also requires
model, platform and platform version
as headers to request. 但是,服务器还需要
model, platform and platform version
作为请求的标头。 How can I add these headers to HTTPRequest. 如何将这些标头添加到HTTPRequest。 At Postman, I can add it in Headers tab.
在Postman,我可以在Headers选项卡中添加它。 Eg.
例如。
Model: Redmi 4 Platform: android
. Model: Redmi 4 Platform: android
。 Feel free to edit to make it clear to others. 随意编辑以使其他人清楚。 Below there is my code who
HTTPRequest
creates: 下面是
HTTPRequest
创建的代码:
Poco::JSON::Object obj;
obj.set("login", "log123");
obj.set("password","pas123");
Poco::Net::HTTPClientSession session("http://hostAddress",
80); //giving host name and port number
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST,
"http://requestAddress","1.1"); //initializing request body
Poco::Net::HTTPResponse response;
std::stringstream ss;
obj.stringify(ss);
request.setContentType("application/json");
request.setContentLength(ss.str().length());
std::ostream& oStream = session.sendRequest(request);// sends request, returns open stream
obj.stringify(oStream);
std::istream& iStream = session.receiveResponse(response);
I tried to find some information at https://pocoproject.org/docs/Poco.Net.HTTPRequest.html . 我试图在https://pocoproject.org/docs/Poco.Net.HTTPRequest.html上找到一些信息。 https://pocoproject.org/docs/Poco.Net.HTTPMessage.html .
https://pocoproject.org/docs/Poco.Net.HTTPMessage.html 。 But without results.
但没有结果。
There is one solution. 有一个解决方案。 It can help to others.
它可以帮助别人。
request.add("string:key","string:value") //In order to add headers to request.
Eg: 例如:
request.add("X-Make","Xiaomi");
request.add("X-Model","Redmi 4");
request.add("X-Platform","android");
request.add("X-Platform-Version","6.0.1");
It works for me. 这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.