简体   繁体   English

Poco HTTPClientSession将标头添加到HTTPRequest

[英]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.

相关问题 从Poco HTTPClientSession异步读取 - Async read from Poco HTTPClientSession 如何在Poco :: HTTPClientSession中获得下载大小? - How to get download size in Poco::HTTPClientSession? Poco::Net::HTTPClientSession 在没有 unique_ptr 的情况下抛出异常 - Poco::Net::HTTPClientSession throw exception without unique_ptr Poco :: Net :: HTTPClientSession json数据未收到Content-Type - Poco::Net::HTTPClientSession json data Content-Type not received Poco :: HttpClientSession.receiveResponse()抛出NoMessageException没有任何明显的原因 - Poco::HttpClientSession.receiveResponse() throws NoMessageException without any apparent reason 在 poco 1.10.1 中发送 HTTPRequest 超时 - Timeout sending HTTPRequest in poco 1.10.1 在 C++ 中使用 Poco::Net::HTTPClientSession 获取简单 GET 方法 REST API 的 BAD REQUEST 错误 - getting BAD REQUEST error for simple GET method REST API using Poco::Net::HTTPClientSession in C++ 如何将Poco :: Net :: HTTPClientSession的套接字设置为TCP_NODELAY? - How do I set a Poco::Net::HTTPClientSession's socket to TCP_NODELAY? Poco HTTPClientSession.receiveResponse返回状态200,但在c ++ 11中为空响应? - Poco HTTPClientSession.receiveResponse returns status 200 but empty response in c++11? 在Linux上添加POCO C ++库Netbeans - Adding POCO C++ Library Netbeans on Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM