Skip to content

Commit a0c403b

Browse files
author
zhangbin4
committed

12 files changed

+593
-32
lines changed

HTTP_SIMPLE_CLIENT/HTTP_SIMPLE_CLIENT.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// HTTP_SIMPLE_CLIENT.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
22
//
3-
3+
#include "def.h"
44
//#include <iostream>
55
//
66
//int main()
@@ -19,16 +19,13 @@
1919
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
2020
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
2121
//https://segmentfault.com/q/1010000002869889
22-
#include "cpprest/filestream.h"
23-
#include "cpprest/http_client.h"
2422

25-
using namespace utility; // Common utilities like string conversions
26-
using namespace web; // Common features like URIs.
27-
using namespace web::http; // Common HTTP functionality
28-
using namespace web::http::client; // HTTP client features
29-
using namespace concurrency::streams; // Asynchronous streams
3023

31-
int main(int argc, char* argv[])
24+
//#if USE_SIMPLE_CLIENT
25+
//int main(int argc, char* argv[])
26+
//#else
27+
int main_simple(int argc, char* argv[])
28+
//#endif
3229
{
3330
auto fileStream = std::make_shared<ostream>();
3431
// Open stream to output file.

HTTP_SIMPLE_CLIENT/HTTP_SIMPLE_CLIENT.vcxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@
155155
</Link>
156156
</ItemDefinitionGroup>
157157
<ItemGroup>
158+
<ClCompile Include="full_httpclient.cpp" />
158159
<ClCompile Include="HTTP_SIMPLE_CLIENT.cpp" />
160+
<ClCompile Include="main.cpp" />
161+
</ItemGroup>
162+
<ItemGroup>
163+
<ClInclude Include="def.h" />
159164
</ItemGroup>
160165
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
161166
<ImportGroup Label="ExtensionTargets">

HTTP_SIMPLE_CLIENT/HTTP_SIMPLE_CLIENT.vcxproj.filters

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,16 @@
1818
<ClCompile Include="HTTP_SIMPLE_CLIENT.cpp">
1919
<Filter>源文件</Filter>
2020
</ClCompile>
21+
<ClCompile Include="full_httpclient.cpp">
22+
<Filter>源文件</Filter>
23+
</ClCompile>
24+
<ClCompile Include="main.cpp">
25+
<Filter>源文件</Filter>
26+
</ClCompile>
27+
</ItemGroup>
28+
<ItemGroup>
29+
<ClInclude Include="def.h">
30+
<Filter>头文件</Filter>
31+
</ClInclude>
2132
</ItemGroup>
2233
</Project>

HTTP_SIMPLE_CLIENT/def.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "cpprest/filestream.h"
4+
#include "cpprest/http_client.h"
5+
6+
using namespace utility; // Common utilities like string conversions
7+
using namespace web; // Common features like URIs.
8+
using namespace web::http; // Common HTTP functionality
9+
using namespace web::http::client; // HTTP client features
10+
using namespace concurrency::streams; // Asynchronous streams
11+
12+
13+
#include <cpprest/http_client.h>
14+
#include <cpprest/json.h>
15+
//#pragma comment(lib, "cpprest_2_10")
16+
//
17+
//using namespace web;
18+
//using namespace web::http;
19+
//using namespace web::http::client;
20+
//
21+
//#include <iostream>
22+
//using namespace std;
23+
24+
25+
26+
int main_simple(int argc, char* argv[]);
27+
int full_client_main();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
//https://mariusbancila.ro/blog/2017/11/19/revisited-full-fledged-client-server-example-with-c-rest-sdk-2-10/
3+
#include "def.h"
4+
/*
5+
Notice there are slight changes in the way output is formatted, both in the server and the client application. The rest
6+
is mostly unchanged, except for the handling of JSON, that has changed significatly since version 1.1. Again, please see
7+
the original post for an explanation of the code.
8+
9+
The output from running these client and server applications is shown below. On the left is the client output, and on
10+
the right the server output.
11+
*/
12+
void display_json(json::value const& jvalue, utility::string_t const& prefix)
13+
{
14+
std::wcout << prefix << jvalue.serialize() << std::endl;
15+
}
16+
17+
pplx::task<http_response> make_task_request(http_client& client, method mtd, json::value const& jvalue)
18+
{
19+
return (mtd == methods::GET || mtd == methods::HEAD) ? client.request(mtd, L"/restdemo")
20+
: client.request(mtd, L"/restdemo", jvalue);
21+
}
22+
23+
void make_request(http_client& client, method mtd, json::value const& jvalue)
24+
{
25+
make_task_request(client, mtd, jvalue)
26+
.then([](http_response response) {
27+
if (response.status_code() == status_codes::OK)
28+
{
29+
return response.extract_json();
30+
}
31+
return pplx::task_from_result(json::value());
32+
})
33+
.then([](pplx::task<json::value> previousTask) {
34+
try
35+
{
36+
display_json(previousTask.get(), L"R: ");
37+
}
38+
catch (http_exception const& e)
39+
{
40+
std::wcout << e.what() << std::endl;
41+
}
42+
})
43+
.wait();
44+
}
45+
46+
int full_client_main()
47+
{
48+
http_client client(U("http://localhost:8888"));
49+
50+
auto putvalue = json::value::object();
51+
putvalue[L"one"] = json::value::string(L"100");
52+
putvalue[L"two"] = json::value::string(L"200");
53+
54+
std::wcout << L"\nPUT (add values)\n";
55+
display_json(putvalue, L"S: ");
56+
make_request(client, methods::PUT, putvalue);
57+
58+
auto getvalue = json::value::array();
59+
getvalue[0] = json::value::string(L"one");
60+
getvalue[1] = json::value::string(L"two");
61+
getvalue[2] = json::value::string(L"three");
62+
63+
std::wcout << L"\nPOST (get some values)\n";
64+
display_json(getvalue, L"S: ");
65+
make_request(client, methods::POST, getvalue);
66+
67+
auto delvalue = json::value::array();
68+
delvalue[0] = json::value::string(L"one");
69+
70+
std::wcout << L"\nDELETE (delete values)\n";
71+
display_json(delvalue, L"S: ");
72+
make_request(client, methods::DEL, delvalue);
73+
74+
std::wcout << L"\nPOST (get some values)\n";
75+
display_json(getvalue, L"S: ");
76+
make_request(client, methods::POST, getvalue);
77+
78+
auto nullvalue = json::value::null();
79+
80+
std::wcout << L"\nGET (get all values)\n";
81+
display_json(nullvalue, L"S: ");
82+
make_request(client, methods::GET, nullvalue);
83+
84+
return 0;
85+
}

HTTP_SIMPLE_CLIENT/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
#include "def.h"
4+
5+
int main(int argc ,char * argv[])
6+
{
7+
full_client_main();
8+
9+
return 0;
10+
11+
}

0 commit comments

Comments
 (0)