C++ 卷曲与静止
我正在尝试使用CPPREST http_客户端访问URL: 我正在接收URL重定向的响应代码302 但是当我尝试使用CURL访问相同的URL时,我收到了CURLE_OK 下面是两段代码: 使用CURL: 及 及 但响应与cpp rest的302相同。[供交叉核对] 工作正常吗 更新2: @Matt Weber解释的方法似乎非常有用和合法,但我不断得到错误代码:400,因此我尝试了以下方法: 我试图在uri_builder中设置URL的主机和端口C++ 卷曲与静止,c++,libcurl,casablanca,C++,Libcurl,Casablanca,我正在尝试使用CPPREST http_客户端访问URL: 我正在接收URL重定向的响应代码302 但是当我尝试使用CURL访问相同的URL时,我收到了CURLE_OK 下面是两段代码: 使用CURL: 及 及 但响应与cpp rest的302相同。[供交叉核对] 工作正常吗 更新2: @Matt Weber解释的方法似乎非常有用和合法,但我不断得到错误代码:400,因此我尝试了以下方法: 我试图在uri_builder中设置URL的主机和端口 http_client client(U("ht
http_client client(U("http://www.20min.ch/rss/"));
uri_builder builder(U("/rss.tmpl"));
builder.append_query(U("type"), U("channel"));
builder.append_query(U("get"), U("68"));
builder.set_host(U("www.20min.ch"));
builder.set_port(U("80"));
client.request(methods::GET, builder.to_string()).then([=](http_response response)
{
cout<<"Received response status code: "<<response.status_code();
});
http_客户端(U)http://www.20min.ch/rss/"));
uri_生成器(U(“/rss.tmpl”);
附加查询(U(“类型”)、U(“频道”);
附加查询(U(“get”)、U(“68”);
builder.set_host(U(“www.20min.ch”);
建造商。设置港口(U(“80”);
client.request(方法::GET,builder.to_string())。然后([=](http_响应)
{
coutRest SDK代码的问题在于http\U客户端
初始化:
http_client client1(U(url_));
U
宏与字符串文字一起使用,以生成可用于构造uri
的内容。如果您在Windows上,则不应编译该内容,因为宏扩展会导致Lurl\U
。显然,无论在您的系统上产生什么结果,都会导致请求以302响应的内容
有两种选择。一种是直接使用文字:
http_client client1(U("http://www.20min.ch/rss/rss.tmpl?type=channel&get=68"));
如果要保留std::string
并从中初始化客户端,可以将其转换为utility::string\t
,从中可以构造uri
std::string url_= "http://www.20min.ch/rss/rss.tmpl?type=channel&get=68";
http_client client1(utility::conversions::to_string_t(url_));
完成此操作后,您可能会发现您需要调用request
的continuation上的wait
函数,以便实际查看预期的输出:
client1.request(methods::GET, builder1.to_string()).then([](http_response response)
{
cout<<"Response code is : "<<response.status_code();
}).wait(); // ensure that the response gets processed
Linux请求:
GET /rss/rss.tmpl?type=channel&get=68 HTTP/1.1\r\n
Host: www.20min.ch:80\r\n
User-Agent:cpprestsdk/2.8.0\r\n
Connection: Keep-Alive\r\n
\r\n
您可以通过wget验证这是原因:
$wget--header=“主机:www.20min.ch”-S”http://www.20min.ch/rss/rss.tmpl?type=channel&get=68“
HTTP/1.1200ok
$wget--header=“主机:www.20min.ch:80”-S”http://www.20min.ch/rss/rss.tmpl?type=channel&get=68“--最大重定向0
找到HTTP/1.1 302
标头的差异是由于不同的实现。WinHTTP客户端实现没有显式添加主机标头,可能是因为它依赖WinHTTP在内部进行添加。不过,asio客户端实现确实添加了主机标头
// Add the Host header if user has not specified it explicitly
if (!ctx->m_request.headers().has(header_names::host))
{
request_stream << "Host: " << host << ":" << port << CRLF;
}
//如果用户未明确指定主机头,请添加主机头
如果(!ctx->m_request.headers()具有(header_names::host))
{
request_stream Hi..非常感谢,我的系统正在等待输出..这只是我为解释问题而编写的最低代码。我正在linux上运行..我将尝试您的解决方案,并让您知道我尝试了:“http_客户端客户端1(实用工具::转换::to_字符串_t(url));”但同样的响应302我也尝试了http_客户端1(u(“);但响应相同..m基于Linux构建如果将http_客户端初始化为U('http://www.20min.ch/“
?如果返回的是200,请查看示例,并尝试像该示例一样使用uri_生成器处理URL的其余部分。好的,我相信我现在已经找到了答案。编辑了我的答案。
http_client client1(U("http://www.20min.ch/rss/rss.tmpl?type=channel&get=68"));
std::string url_= "http://www.20min.ch/rss/rss.tmpl?type=channel&get=68";
http_client client1(utility::conversions::to_string_t(url_));
client1.request(methods::GET, builder1.to_string()).then([](http_response response)
{
cout<<"Response code is : "<<response.status_code();
}).wait(); // ensure that the response gets processed
GET /rss/rss.tmpl?type=channel&get=68 HTTP/1.1\r\n
Connection: Keep-Alive\r\n
User-Agent: cpprestsdk/2.8.0\r\n
Host: www.20min.ch\r\n
\r\n
GET /rss/rss.tmpl?type=channel&get=68 HTTP/1.1\r\n
Host: www.20min.ch:80\r\n
User-Agent:cpprestsdk/2.8.0\r\n
Connection: Keep-Alive\r\n
\r\n
// Add the Host header if user has not specified it explicitly
if (!ctx->m_request.headers().has(header_names::host))
{
request_stream << "Host: " << host << ":" << port << CRLF;
}
std::string url_= "http://www.20min.ch/rss/rss.tmpl?type=channel&get=68";
http_client client1(utility::conversions::to_string_t(url_));
http_request request;
request.set_method(methods::GET);
request.headers().add(U("Host"), U("www.20min.ch"));
client1.request(request).then([](http_response response)
{
std::cout<<"Response code is : "<<response.status_code();
}).wait();