acl/lib_acl_cpp/samples/http/http_request/main.cpp

117 lines
2.3 KiB
C++
Raw Normal View History

2019-08-06 14:32:54 +08:00
#include "lib_acl.h"
2015-02-04 22:26:20 +08:00
#include "acl_cpp/lib_acl.hpp"
static void usage(const char* procname)
{
printf("usage: %s -h [help] \r\n"
"\t-s server_addr[127.0.0.1:8194] \r\n"
"\t-n max_loop \r\n"
"\t-z[accept_gzip]\r\n"
"\t-B[send_body data]\r\n", procname);
2015-02-04 22:26:20 +08:00
}
int main(int argc, char* argv[])
{
int ch, max = 10;
bool accept_gzip = false, send_body = false;
2015-02-04 22:26:20 +08:00
acl::string addr("127.0.0.1:8194");
2019-08-06 14:32:54 +08:00
acl::acl_cpp_init();
while ((ch = getopt(argc, argv, "hs:n:zB")) > 0)
2015-02-04 22:26:20 +08:00
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'n':
max = atoi(optarg);
break;
case 'z':
accept_gzip = true;
break;
case 'B':
send_body = true;
break;
2015-02-04 22:26:20 +08:00
default:
break;
}
}
acl::log::stdout_open(true);
2015-02-04 22:26:20 +08:00
acl::string buf(1024);
for (size_t i = 0; i < 1024; i++)
buf << 'X';
acl::http_request req(addr, 10, 10);
acl::string tmp(1024);
for (int i = 0; i < max; i++)
{
acl::http_header& header = req.request_header();
//header.set_method(acl::HTTP_METHOD_POST);
2015-02-04 22:26:20 +08:00
header.set_url("/");
header.set_keep_alive(true);
header.accept_gzip(accept_gzip ? true : false);
//header.set_content_length(buf.length());
bool rc;
if (send_body)
rc = req.request(buf.c_str(), buf.length());
else
rc = req.request(NULL, 0);
2015-02-04 22:26:20 +08:00
// 只所以将 build_request 放在 req.request 后面,是因为
// req.request 内部可能会修改请求头中的字段
acl::string hdr;
header.build_request(hdr);
printf("request header:\r\n%s\r\n", hdr.c_str());
if (rc == false)
2015-02-04 22:26:20 +08:00
{
printf("send request error\n");
break;
}
printf("send request ok\r\n");
2015-02-04 22:26:20 +08:00
tmp.clear();
int size = 0, real_size = 0, n;
2015-02-04 22:26:20 +08:00
while (true)
{
int ret = req.read_body(tmp, false, &n);
2015-02-04 22:26:20 +08:00
if (ret < 0) {
printf("read_body error\n");
return 1;
}
else if (ret == 0)
{
printf("-------------read over---------\r\n");
2015-02-04 22:26:20 +08:00
break;
}
2015-02-04 22:26:20 +08:00
size += ret;
real_size += n;
2015-02-04 22:26:20 +08:00
}
printf("read body size: %d, real_size: %d, %s\n",
size, real_size, tmp.c_str());
printf("===============================================\r\n");
2015-02-04 22:26:20 +08:00
}
2019-08-06 14:32:54 +08:00
#if defined(_WIN32) || defined(_WIN64)
printf("Enter any key to exit ...");
fflush(stdout);
getchar();
#endif
2015-02-04 22:26:20 +08:00
return 0;
}