我正在尝试在我的Ubuntu环境中构建一个在Microsoft Casablanca中提供的示例。 我正在关注链接: https://msdn.microsoft.com/en-us/library/jj950082.aspx
我的代码来自上面的链接:
#include <http_client.h>
#include <iostream>
#include <json.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace std;
// Retrieves a JSON value from an HTTP request.
pplx::task<void> RequestJSONValueAsync()
{
// TODO: To successfully use this example, you must perform the request
// against a server that provides JSON data.
// This example fails because the returned Content-Type is text/html and not application/json.
http_client client(U("http://www.fourthcoffee.com"));
return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
{
if(response.status_code() == status_codes::OK)
{
return response.extract_json();
}
// Handle error cases, for now return empty json value...
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
const json::value& v = previousTask.get();
// Perform actions here to process the JSON value...
}
catch (const http_exception& e)
{
// Print error.
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});
/* Output:
Content-Type must be application/json to extract (is: text/html)
*/
}
int main()
{
// This example uses the task::wait method to ensure that async operations complete before the app exits.
// In most apps, you typically don�t wait for async operations to complete.
wcout << U("Calling RequestJSONValueAsync..." )<< endl;
RequestJSONValueAsync().wait();
}
这里我尝试向网址发送GET请求:http://www.fourthcoffee.com并获取JSON数据。
当我使用终端中的命令编译并运行它时:
sudo g ++ -std = c ++ 11 basic_http_client.cpp -o client -lboost_system -lcrypto -lssl -lcpprest
./client
我面临一个错误:
错误:无法连接到任何已解析的端点。
在发出http GET请求时,这可能是什么问题。搜索谷歌和SO没有可能的解决方案。