-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
72 lines (53 loc) · 1.35 KB
/
http_client.cpp
File metadata and controls
72 lines (53 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* cpp-httplib sample
* 2020-07-01 K.OHWADA
*/
// HTTP Client
// original : https://github.com/yhirose/cpp-httplib/blob/master/example/simplecli.cc
//
// simplecli.cc
//
// Copyright (c) 2019 Yuji Hirose. All rights reserved.
// MIT License
//
#include <iostream>
#include "httplib.h"
using namespace std;
/**
* main
*/
int main(int argc, const char *argv[])
{
string host = "example.com";
int port = 80;
string path = "/";
if (argc == 4) {
host = argv[1];
port = atoi(argv[2]);
path = argv[3];
} else if (argc == 3) {
host = argv[1];
port = atoi(argv[2]);
} else if (argc == 2) {
host = argv[1];
} else {
cerr << "usage: " << argv[0] << " [host] [port] [path] " << endl;
}
cerr << "host: " << host << endl;
cerr << "port: " << port << endl;
cerr << "path: " << path << endl;
httplib::Client client( host, port );
auto res = client.Get( path.c_str() );
if ( res ) {
v cerr << endl;
cerr << "Status: " << res->status << endl;
cerr << "Content-Type: " << res->get_header_value("Content-Type") << endl;
cout << res->body << endl;
} else {
printError( res.error() );
}
return EXIT_SUCCESS;
}
// Status: 200
// Content-Type: text/html; charset=UTF-8
// <title>Example Domain</title>