MCPcopy Create free account
hub / github.com/apache/brpc / main

Function main

example/http_c++/http_client.cpp:39–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37}
38
39int main(int argc, char* argv[]) {
40 // Parse gflags. We recommend you to use gflags as well.
41 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
42
43 if (argc != 2) {
44 LOG(ERROR) << "Usage: ./http_client \"http(s)://www.foo.com\"";
45 return -1;
46 }
47 char* url = argv[1];
48
49 // A Channel represents a communication line to a Server. Notice that
50 // Channel is thread-safe and can be shared by all threads in your program.
51 brpc::Channel channel;
52 brpc::ChannelOptions options;
53 options.protocol = FLAGS_protocol;
54 options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
55 options.max_retry = FLAGS_max_retry;
56
57 // Initialize the channel, NULL means using default options.
58 // options, see `brpc/channel.h'.
59 if (channel.Init(url, FLAGS_load_balancer.c_str(), &options) != 0) {
60 LOG(ERROR) << "Fail to initialize channel";
61 return -1;
62 }
63
64 // We will receive response synchronously, safe to put variables
65 // on stack.
66 brpc::Controller cntl;
67
68 cntl.http_request().uri() = url;
69 if (!FLAGS_d.empty()) {
70 cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
71 cntl.request_attachment().append(FLAGS_d);
72 }
73
74 // Because `done'(last parameter) is NULL, this function waits until
75 // the response comes back or error occurs(including timedout).
76 channel.CallMethod(NULL, &cntl, NULL, NULL, NULL);
77 if (cntl.Failed()) {
78 std::cerr << cntl.ErrorText() << std::endl;
79 return -1;
80 }
81 // If -http_verbose is on, brpc already prints the response to stderr.
82 if (!brpc::FLAGS_http_verbose) {
83 std::cout << cntl.response_attachment() << std::endl;
84 }
85 return 0;
86}

Callers

nothing calls this directly

Calls 8

ErrorTextMethod · 0.80
InitMethod · 0.45
c_strMethod · 0.45
emptyMethod · 0.45
set_methodMethod · 0.45
appendMethod · 0.45
CallMethodMethod · 0.45
FailedMethod · 0.45

Tested by

no test coverage detected