Skip to content

Commit d01da45

Browse files
author
zhangbin4
committed
simple http client and server
1 parent 75f2587 commit d01da45

File tree

8 files changed

+507
-1
lines changed

8 files changed

+507
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ WIN32/Release/
55
WIN32/.vs/
66
.vscode/
77
HTTP_CLIENT_GET_PICTURE/Release/
8-
8+
HTTP_SIMPLE_CLIENT/Release/
9+
HTTP_SIMPLE_SERVER/Release/
910
# Visual Studio files
1011
*.o
1112
*.d
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// HTTP_SIMPLE_CLIENT.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
2+
//
3+
4+
//#include <iostream>
5+
//
6+
//int main()
7+
//{
8+
// std::cout << "Hello World!\n";
9+
//}
10+
11+
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
12+
// 调试程序: F5 或调试 >“开始调试”菜单
13+
14+
// 入门使用技巧:
15+
// 1. 使用解决方案资源管理器窗口添加/管理文件
16+
// 2. 使用团队资源管理器窗口连接到源代码管理
17+
// 3. 使用输出窗口查看生成输出和其他消息
18+
// 4. 使用错误列表窗口查看错误
19+
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
20+
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
21+
//https://segmentfault.com/q/1010000002869889
22+
#include "cpprest/filestream.h"
23+
#include "cpprest/http_client.h"
24+
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
30+
31+
int main(int argc, char* argv[])
32+
{
33+
auto fileStream = std::make_shared<ostream>();
34+
// Open stream to output file.
35+
pplx::task<void> requestTask = fstream::open_ostream(U("results.html"))
36+
.then([=](ostream outFile) {
37+
*fileStream = outFile;
38+
// Create http_client to send the request.
39+
http_client client(U("http://www.bing.com/"));
40+
http_client localclient(U("http://localhost:8888"));
41+
printf("create client and send request \n");
42+
return localclient.request(methods::GET);
43+
})
44+
.then([=](http_response response) {
45+
printf("Received response status code:%u\n", response.status_code());
46+
system("pause");
47+
return response.body().read_to_end(fileStream->streambuf());
48+
})
49+
.then([=](size_t) {
50+
printf("close file stream \n");
51+
return fileStream->close(); });
52+
try
53+
{
54+
requestTask.wait();
55+
}
56+
catch (const std::exception& e)
57+
{
58+
printf("Error exception:%s\n", e.what());
59+
system("pause");
60+
}
61+
62+
return 0;
63+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<ProjectGuid>{05071389-F9F3-4C44-B155-A15E43CA3FFD}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>HTTPSIMPLECLIENT</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v142</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v142</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v142</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v142</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
74+
<LinkIncremental>false</LinkIncremental>
75+
<OutDir>$(SolutionDir)$(Configuration)\Binaries\Release</OutDir>
76+
</PropertyGroup>
77+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
78+
<LinkIncremental>true</LinkIncremental>
79+
</PropertyGroup>
80+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
81+
<LinkIncremental>true</LinkIncremental>
82+
</PropertyGroup>
83+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
84+
<LinkIncremental>false</LinkIncremental>
85+
</PropertyGroup>
86+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
87+
<ClCompile>
88+
<PrecompiledHeader>
89+
</PrecompiledHeader>
90+
<WarningLevel>Level3</WarningLevel>
91+
<Optimization>MaxSpeed</Optimization>
92+
<FunctionLevelLinking>true</FunctionLevelLinking>
93+
<IntrinsicFunctions>true</IntrinsicFunctions>
94+
<SDLCheck>true</SDLCheck>
95+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
96+
<ConformanceMode>true</ConformanceMode>
97+
<AdditionalIncludeDirectories>../Release/include</AdditionalIncludeDirectories>
98+
</ClCompile>
99+
<Link>
100+
<SubSystem>Console</SubSystem>
101+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
102+
<OptimizeReferences>true</OptimizeReferences>
103+
<GenerateDebugInformation>true</GenerateDebugInformation>
104+
<AdditionalLibraryDirectories>../WIN32/Release/Binaries/Release</AdditionalLibraryDirectories>
105+
<AdditionalDependencies>..\WIN32\Release\Binaries\Release\cpprest140_2_10.lib;%(AdditionalDependencies)</AdditionalDependencies>
106+
</Link>
107+
</ItemDefinitionGroup>
108+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
109+
<ClCompile>
110+
<PrecompiledHeader>
111+
</PrecompiledHeader>
112+
<WarningLevel>Level3</WarningLevel>
113+
<Optimization>Disabled</Optimization>
114+
<SDLCheck>true</SDLCheck>
115+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
116+
<ConformanceMode>true</ConformanceMode>
117+
</ClCompile>
118+
<Link>
119+
<SubSystem>Console</SubSystem>
120+
<GenerateDebugInformation>true</GenerateDebugInformation>
121+
</Link>
122+
</ItemDefinitionGroup>
123+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
124+
<ClCompile>
125+
<PrecompiledHeader>
126+
</PrecompiledHeader>
127+
<WarningLevel>Level3</WarningLevel>
128+
<Optimization>Disabled</Optimization>
129+
<SDLCheck>true</SDLCheck>
130+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
131+
<ConformanceMode>true</ConformanceMode>
132+
</ClCompile>
133+
<Link>
134+
<SubSystem>Console</SubSystem>
135+
<GenerateDebugInformation>true</GenerateDebugInformation>
136+
</Link>
137+
</ItemDefinitionGroup>
138+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
139+
<ClCompile>
140+
<PrecompiledHeader>
141+
</PrecompiledHeader>
142+
<WarningLevel>Level3</WarningLevel>
143+
<Optimization>MaxSpeed</Optimization>
144+
<FunctionLevelLinking>true</FunctionLevelLinking>
145+
<IntrinsicFunctions>true</IntrinsicFunctions>
146+
<SDLCheck>true</SDLCheck>
147+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
148+
<ConformanceMode>true</ConformanceMode>
149+
</ClCompile>
150+
<Link>
151+
<SubSystem>Console</SubSystem>
152+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
153+
<OptimizeReferences>true</OptimizeReferences>
154+
<GenerateDebugInformation>true</GenerateDebugInformation>
155+
</Link>
156+
</ItemDefinitionGroup>
157+
<ItemGroup>
158+
<ClCompile Include="HTTP_SIMPLE_CLIENT.cpp" />
159+
</ItemGroup>
160+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
161+
<ImportGroup Label="ExtensionTargets">
162+
</ImportGroup>
163+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="源文件">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="头文件">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="资源文件">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="HTTP_SIMPLE_CLIENT.cpp">
19+
<Filter>源文件</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

HTTP_SIMPLE_CLIENT/results.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, World!
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// HTTP_SIMPLE_SERVER.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
2+
//
3+
4+
//#include <iostream>
5+
//
6+
//int main()
7+
//{
8+
// std::cout << "Hello World!\n";
9+
//}
10+
11+
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
12+
// 调试程序: F5 或调试 >“开始调试”菜单
13+
14+
// 入门使用技巧:
15+
// 1. 使用解决方案资源管理器窗口添加/管理文件
16+
// 2. 使用团队资源管理器窗口连接到源代码管理
17+
// 3. 使用输出窗口查看生成输出和其他消息
18+
// 4. 使用错误列表窗口查看错误
19+
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
20+
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
21+
//https : // segmentfault.com/q/1010000002869889
22+
23+
24+
#include "cpprest/asyncrt_utils.h"
25+
#include "cpprest/http_client.h"
26+
#include "cpprest/http_listener.h"
27+
#include "cpprest/json.h"
28+
#include "cpprest/uri.h"
29+
using namespace web::http::experimental::listener;
30+
using namespace web::http;
31+
using namespace web;
32+
33+
void handle_get(http_request message) { message.reply(status_codes::OK, U("Hello, World!")); };
34+
35+
void handle_post(http_request message) { message.reply(status_codes::NotFound); };
36+
37+
void handle_put(http_request message) { message.reply(status_codes::NotFound); };
38+
39+
void handle_delete(http_request message) { message.reply(status_codes::NotFound); };
40+
41+
#define TRACE(msg) std::wcout << msg
42+
#define TRACE_ACTION(a, k, v) std::wcout << a << L" (" << k << L", " << v << L")\n"
43+
44+
int main(int argc, char** argv)
45+
{
46+
uri_builder uri(U("http://localhost:8888"));
47+
http_listener listener(uri.to_uri());
48+
49+
listener.support(methods::GET, handle_get);
50+
listener.support(methods::POST, handle_post);
51+
listener.support(methods::PUT, handle_put);
52+
listener.support(methods::DEL, handle_delete);
53+
54+
try
55+
{
56+
listener.open().then([&listener]() { TRACE(L"\nstarting to listen\n"); }).wait();
57+
58+
while (true)
59+
;
60+
}
61+
catch (std::exception const& e)
62+
{
63+
std::wcout << e.what() << std::endl;
64+
}
65+
catch (...)
66+
{
67+
std::wcout << "Unknown exception" << std::endl;
68+
}
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)