更新
我正在使用 cpp-netlib (v0.11.0) 发送 HTTP 请求。
以下代码使用给定的正文发送 HTTP POST 请求。
client httpClient;
try
{
uri::uri url;
url << uri::scheme("http")
<< uri::host(m_hostname)
<< uri::port(m_port)
<< uri::path(m_path);
// create a request instance and configure the headers
client::request request(url);
request << header("Connection", "close");
request << header("Content-Type", "application/x-www-form-urlencoded");
// send the request
client::response response = httpClient.post(request, "foo=bar");
}
catch (std::exception& ex)
{
...
}
但是,以下代码会导致错误请求。
client httpClient;
try
{
uri::uri url;
url << uri::scheme("http")
<< uri::host(m_hostname)
<< uri::port(m_port)
<< uri::path(m_path)
<< uri::query("foo", "bar");
// create a request instance and configure the headers
client::request request(url);
request << header("Connection", "close");
request << header("Content-Type", "application/x-www-form-urlencoded");
request << body("foo=bar");
// send the request
client::response response = httpClient.post(request);
}
catch (std::exception& ex)
{
...
}
请有人可以解释我在第二个示例中做错了什么,哪个是首选选项。