acl/lib_acl_cpp/samples/http_request/main.cpp

205 lines
4.4 KiB
C++
Raw Normal View History

// http_servlet.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
2014-11-19 00:25:21 +08:00
//
#include <assert.h>
#include <getopt.h>
#include "acl_cpp/lib_acl.hpp"
//////////////////////////////////////////////////////////////////////////
class http_request_test
{
public:
http_request_test(const char* server_addr, const char* file,
const char* stype, const char* charset)
{
server_addr_= server_addr;
file_ = file;
stype_ = stype;
charset_ = charset;
to_charset_ = "gb2312";
}
~http_request_test() {}
bool run(void)
{
acl::string body;
// <20>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (acl::ifstream::load(file_, &body) == false)
{
logger_error("load %s error", file_.c_str());
return false;
}
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>
2014-11-19 00:25:21 +08:00
acl::http_request req(server_addr_);
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD>
2014-11-19 00:25:21 +08:00
acl::string ctype("text/");
ctype << stype_ << "; charset=" << charset_;
acl::http_header& hdr = req.request_header(); // <20><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
hdr.set_url("/");
hdr.set_content_type(ctype);
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (req.request(body.c_str(), body.length()) == false)
{
logger_error("send http request to %s error",
server_addr_.c_str());
return false;
}
// ȡ<><C8A1> HTTP <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
2014-11-19 00:25:21 +08:00
const char* p = req.header_value("Content-Type");
if (p == NULL || *p == 0)
{
logger_error("no Content-Type");
return false;
}
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
acl::http_ctype content_type;
content_type.parse(p);
// <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
const char* stype = content_type.get_stype();
bool ret;
if (stype == NULL)
ret = do_plain(req);
else if (strcasecmp(stype, "xml") == 0)
ret = do_xml(req);
else if (strcasecmp(stype, "json") == 0)
ret = do_json(req);
else
ret = do_plain(req);
if (ret == true)
printf("read ok!\r\n");
return ret;
}
private:
// <20><><EFBFBD><EFBFBD> text/plain <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
bool do_plain(acl::http_request& req)
{
acl::string body;
if (req.get_body(body, to_charset_) == false)
{
logger_error("get http body error");
return false;
}
printf("body:\r\n(%s)\r\n", body.c_str());
return true;
}
// <20><><EFBFBD><EFBFBD> text/xml <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
bool do_xml(acl::http_request& req)
{
acl::xml1 body;
2014-11-19 00:25:21 +08:00
if (req.get_body(body, to_charset_) == false)
{
logger_error("get http body error");
return false;
}
acl::xml_node* node = body.first_node();
while (node)
{
const char* tag = node->tag_name();
const char* name = node->attr_value("name");
const char* pass = node->attr_value("pass");
printf(">>tag: %s, name: %s, pass: %s\r\n",
tag ? tag : "null",
name ? name : "null",
pass ? pass : "null");
node = body.next_node();
}
return true;
}
// <20><><EFBFBD><EFBFBD> text/json <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
bool do_json(acl::http_request& req)
{
acl::json body;
if (req.get_body(body, to_charset_) == false)
{
logger_error("get http body error");
return false;
}
acl::json_node* node = body.first_node();
while (node)
{
if (node->tag_name())
{
printf("tag: %s", node->tag_name());
if (node->get_text())
printf(", value: %s\r\n", node->get_text());
else
printf("\r\n");
}
node = body.next_node();
}
return true;
}
private:
acl::string server_addr_; // web <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
acl::string file_; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
acl::string stype_; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
acl::string charset_; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
acl::string to_charset_; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
2014-11-19 00:25:21 +08:00
};
//////////////////////////////////////////////////////////////////////////
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n", procname);
printf("options:\r\n");
printf("\t-f request file\r\n");
printf("\t-t request stype[xml/json/plain]\r\n");
printf("\t-c request file's charset[gb2312/utf-8]\r\n");
}
int main(int argc, char* argv[])
{
int ch;
acl::string server_addr("127.0.0.1:8888"), file("./xml.txt");
acl::string stype("xml"), charset("gb2312");
while ((ch = getopt(argc, argv, "hs:f:t:c:")) > 0)
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 'f':
file = optarg;
break;
case 't':
stype = optarg;
break;
case 'c':
charset = optarg;
break;
default:
usage(argv[0]);
return 0;
}
}
// <20><><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
2014-11-19 00:25:21 +08:00
acl::log::stdout_open(true);
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
http_request_test request(server_addr, file, stype, charset);
request.run();
return 0;
}