使用 Poco 库获取 URL 参数

get URL params with Poco library

本文关键字:URL 参数 获取 Poco 使用      更新时间:2023-10-16

我正在开发一个带有Poco库的Web服务器。当我的服务器在GET模式下收到带有表单数据的HTTP请求时,我不知道如何使用类HTMLForm显示包含已接收对param=value的列表。

有了request.getURI().getQuery()我能够获得完整的字符串。我想我可以使用分词器以传统方式拆分字符串。

有没有更好的方法来使用 Poco 做到这一点?谢谢

好的,类 HTMLForm 继承自类 NameValueCollection,它实现了一个迭代器,可用于在 "name=value" 对之间移动。

这是解决我问题的代码:

string name;
string value;
HTMLForm form( request );
NameValueCollection::ConstIterator i = form.begin();
while(i!=form.end()){
    name=i->first;
    value=i->second;
    cout << name << "=" << value << endl << flush;
    ++i;
}

使用 poco 版本 1.11.0-all (2021-06-28)您可以这样做:

const Poco::URI Uri(request.getURI());
const Poco::URI::QueryParameters QueryParms = Uri.getQueryParameters();

Poco::URI::QueryParameters是:

std::vector<std::pair<std::string, std::string>>

POCO "NameValueCollection" 几乎与 Vettrasoft Z Directory 相同namevalue_set_o类,记录在此处:

http://www.vettrasoft.com/man/zman-strings-namevalue_set.html

这至少提供了一些示例代码。我遇到的最大问题POCO 缺乏有关如何使用它的示例或解释(包括参考手册页)。对于 Z 目录的名称-值集类,与上述等效的源代码如下所示:

using namespace std;
int i, ie;
namevalue_set_o nv;
string_o s = "FOO=BAR;DATE="12/21/2012";HOST=vertigo;OSTYPE="Windows Vista"";
nv.load_from_string(s);
i = 0;
while (i < nv.size())
{
  const namevalue_pair_o &item = nv.get(i, &ie);
  if (!ie)
    cout << item.name() << "=" item.value() << endl << flush;
  ++i;
}