Refine HTTP request and response.

This commit is contained in:
Adam Gu
2018-07-10 11:02:49 +08:00
parent 88511caff6
commit cbf3efbcba
16 changed files with 116 additions and 138 deletions
+3 -4
View File
@@ -2,8 +2,8 @@
#include "boost/asio/io_context.hpp"
#include "webcc/logger.h"
#include "webcc/async_http_client.h"
#include "webcc/logger.h"
// In order to test this client, create a file index.html whose content is
// simply "Hello, World!", then start a HTTP server with Python 3:
@@ -11,13 +11,12 @@
// The default port number should be 8000.
void Test(boost::asio::io_context& ioc) {
std::shared_ptr<webcc::HttpRequest> request(new webcc::HttpRequest());
webcc::HttpRequestPtr request(new webcc::HttpRequest());
request->set_method(webcc::kHttpGet);
request->set_url("/index.html");
request->SetHost("localhost", "8000");
request->Build();
request->UpdateStartLine();
webcc::HttpAsyncClientPtr client(new webcc::AsyncHttpClient(ioc));
+10 -10
View File
@@ -1,7 +1,7 @@
#include <iostream>
#include "webcc/logger.h"
#include "webcc/http_client.h"
#include "webcc/logger.h"
// In order to test this client, create a file index.html whose content is
// simply "Hello, World!", then start a HTTP server with Python 3:
@@ -10,21 +10,21 @@
void Test() {
webcc::HttpRequest request;
request.set_method(webcc::kHttpGet);
request.set_url("/index.html");
request.SetHost("localhost", "8000");
request.Build();
webcc::HttpResponse response;
request.UpdateStartLine();
webcc::HttpClient client;
if (!client.Request(request)) {
return;
if (client.Request(request)) {
std::cout << client.response()->content() << std::endl;
} else {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
std::cout << response.content() << std::endl;
}
int main() {
+6 -2
View File
@@ -2,8 +2,8 @@
#include "json/json.h"
#include "webcc/logger.h"
#include "webcc/async_rest_client.h"
#include "webcc/logger.h"
// -----------------------------------------------------------------------------
@@ -118,7 +118,11 @@ int main(int argc, char* argv[]) {
if (error == webcc::kNoError) {
std::cout << response->content() << std::endl;
} else {
std::cout << webcc::DescribeError(error) << std::endl;
std::cout << webcc::DescribeError(error);
if (timed_out) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
};