?
#include
#include
using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency;
?
void TestRequest()
{
? ? auto fileStream = std::make_shared();
? ? pplx::task requestTask = concurrency::streams::fstream::open_ostream(U("result.
html")).then([=](concurrency::streams::ostream?
?
outFile){
? ? ? ? *fileStream = outFile;
?
? ? ? ? http_client client(U("http://www.bing.com/"));
? ? ? ? uri_builder builder(U("/search"));
? ? ? ? builder.append_query(U("q"), U("Casablanca CodePlex"));
?
? ? ? ? return client.request(methods::GET, builder.to_string());
? ? })
? ? .then([=](http_response response)
? ? {
? ? ? ? return response.body().read_to_end(fileStream->streambuf());
? ? }).then([=](size_t len){
? ? ? ? return fileStream->close();
? ? });
?
? ? try
? ? {
? ? ? ? requestTask.wait();
? ? }
? ? catch (const std::exception& e)
? ? {
? ? ? ? cout << e.what() << endl;
? ? }
}
这个例子把从bing.com上查询“Casablanca CodePlex”的内容保存到一个本地文件result.html中,用到了ppl的串行任务。启用了四个异步任务,第一个任务是打开一个文件流,接着,发起了第二个任务,用于发起一个查询请求,然后,第三个任务等待请求的响应,并将响应的结果输入到文件中去,第四个任务是关闭文件流。要注意rest sdk的字符相关的入参是宽字符(wchr_t)。这种处理http的方式让我们处理http的流程变得很清晰,有点小清新^_^,不过,这对于不太熟悉ppl用法的童鞋可能有点难接受,没关系,让我来简化一下,简化成同步方式,看得就更清楚了。
?
复制代码
void TestRequest()
{
? ? auto fileStream = std::make_shared();
? ? concurrency::streams::ostream outFile = concurrency::streams::fstream::open_ostream(U("result11.html")).get();
? ? *fileStream = outFile;
?
? ? http_client client(L"http://www.bing.com/");
? ? uri_builder builder(L"/search");
? ? builder.append_query(L"q", L"Casablanca CodePlex");
?
? ? http_response response = client.request(methods::GET, builder.to_string()).get();
? ? response.body().read_to_end(fileStream->streambuf()).get();
? ? fileStream->close().get();
}
复制代码
注意上面的get()方法会阻塞等待异步线程完成操作。这样简化之后就能更清晰的看到如何使用rest sdk了,下面来说说发起http操作的几个对象。 http_client代表客户端,需要它发起http请求。rest api一般是基于一个基本URL增加了一些URL,比如上例中的search,还有可能有一些url参数,这时,我们就需要uri_builder来做这些拼接url和参数的事情,用起来很简单。
?
uri_builder builder;
builder.append_path(L"search"); //添加URL
builder.append_query(L"q", L"Casablanca CodePlex"); //添加url参数
待url和参数准备好之后就可以发起请求了,请求方式可以用methods::GET和methods::POST等方式。
?
client.request(methods::GET, builder.to_string()).get();
上面的例子中并没有request body,有时候我们发起http请求还需要request body,一般是json或者二进制格式,来看一个post json格式的request body的例子,rest sdk提供了json对象来解析json,用起来也很方便:
?
复制代码
uri_builder builder;
builder.append_path(L"/test");
?
json::value obj;
obj[L"Count"] = json::value::number(6);
obj[L"Version"] = json::value::string(L"1.0");
client.request(methods::POST, builder.to_string(), obj.serialize(), L"application/json");
复制代码
如果request body为二进制格式的话,这样发请求就可以了:
?