【Poco】http中传输json对象

2 篇文章 0 订阅

Poco中封装了表单提交的功能,提交表单是非常简单的。

Net::HTMLForm form;
form.add("name", "jack");

URI url("http://httpbin.org/post");

Net::HTTPClientSession session(url.getHost(), url.getPort());
Net::HTTPRequest req(Net::HTTPRequest::HTTP_POST, url.getPathAndQuery());

form.prepareSubmit(req);
auto& ostr = session.sendRequest(req);
form.write(ostr);

Net::HTTPResponse resp;
session.receiveResponse(resp);

 

如今在http中传输json也是比较常见的,但是Poco并没有封装相关接口。

先看看HTMLForm内部做了些什么

void HTMLForm::prepareSubmit(HTTPRequest& request)
{
	if (request.getMethod() == HTTPRequest::HTTP_POST || request.getMethod() == HTTPRequest::HTTP_PUT)
	{
		if (_encoding == ENCODING_URL)
		{
			request.setContentType(_encoding);
			request.setChunkedTransferEncoding(false);
			Poco::CountingOutputStream ostr;
			writeUrl(ostr);
			request.setContentLength(ostr.chars());
		}
		else
		{
			_boundary = MultipartWriter::createBoundary();
			std::string ct(_encoding);
			ct.append("; boundary=\"");
			ct.append(_boundary);
			ct.append("\"");
			request.setContentType(ct);
		}
		if (request.getVersion() == HTTPMessage::HTTP_1_0)
		{
			request.setKeepAlive(false);
			request.setChunkedTransferEncoding(false);
		}
		else if (_encoding != ENCODING_URL)
		{
			request.setChunkedTransferEncoding(true);
		}
	}
	else
	{
		std::string uri = request.getURI();
		std::ostringstream ostr;
		writeUrl(ostr);
		uri.append("?");
		uri.append(ostr.str());
		request.setURI(uri);
	}
}

很简单,只有在使用了post并且content-type为"application/x-www-form-urlencoded"时才会写入表单,所以我们可以直接操作Request。

JSON::Object jo;
jo.set("name", "jack");

std::ostringstream ss;
jo.stringify(ss);
std::string s;
s = ss.str();

URI url("http://httpbin.org/post");

Net::HTTPClientSession session(url.getHost(), url.getPort());
Net::HTTPRequest req(Net::HTTPRequest::HTTP_POST, url.getPathAndQuery());

//form.prepareSubmit(req);
req.setChunkedTransferEncoding(false);
req.setContentType("application/json");
req.setContentLength(s.length());

session.sendRequest(req) << s;

Net::HTTPResponse resp;
session.receiveResponse(resp);

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!感谢你的提问。你想了解如何使用 Poco JSON 吗?Poco 是一个 C++ 开发库,其包含了处理 JSON 数据的功能。使用 Poco JSON,你可以方便地解析和生成 JSON 数据。以下是一个简单的示例演示如何使用 Poco JSON: ```cpp #include <iostream> #include <Poco/JSON/Parser.h> #include <Poco/JSON/Object.h> int main() { std::string jsonString = "{\"name\": \"John\", \"age\": 30}"; Poco::JSON::Parser parser; Poco::Dynamic::Var result = parser.parse(jsonString); Poco::JSON::Object::Ptr jsonObject = result.extract<Poco::JSON::Object::Ptr>(); std::string name = jsonObject->getValue<std::string>("name"); int age = jsonObject->getValue<int>("age"); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; return 0; } ``` 在上面的示例,我们首先定义了一个包含 JSON 数据的字符串。然后,我们使用 Poco::JSON::Parser 对象解析该字符串,并将结果存储在 Poco::Dynamic::Var 对象。接下来,我们从这个对象提取出 Poco::JSON::Object::Ptr 对象,这是一个指向解析后 JSON 对象的智能指针。 我们可以使用 Poco::JSON::Object::Ptr 对象的方法来访问 JSON 数据的属性值。在这个例子,我们获取了 "name" 和 "age" 属性的值,并将它们打印出来。 这只是 Poco JSON 的基本用法,你可以根据自己的需求进一步探索 Poco JSON 的功能。希望这能对你有所帮助!如有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值