Poco库http客户端代码

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  1. 下面是GET请求代码:

//make a http communication request/response
bool doRequest(Poco::Net::HTTPClientSession& session,
        Poco::Net::HTTPRequest& request, Poco::Net::HTTPResponse& response) throw(Poco::Exception){
    try
    {
    session.sendRequest(request);
    std::istream& rs = session.receiveResponse(response);
    std::cout << response.getStatus() << " " << response.getReason()
            << std::endl;
    cout<<"13"<if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED) {
        StreamCopier::copyStream(rs, std::cout);
        cout << "success doRequest:" << response.getStatus() << endl;
        return true;
    } else {
        Poco::NullOutputStream null;
        StreamCopier::copyStream(rs, std::cout);
        cout << "failed doRequest" << response.getStatus() << endl;
        return false;
    }
    }catch(Poco::Exception &e ){
        cout<<"send..... "<return true;
}

bool HttpSender::get(void) {
    try {
        URI uri(serverURL);
        std::string path(uri.getPathAndQuery());
        if (path.empty())
            path = "/";

        HTTPClientSession session(uri.getHost(), uri.getPort());
        HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
        HTTPResponse response;
        //发起300次请求
        for (int i=0; i < 300; i++) {
            if (!doRequest(session, request, response)) {
                std::cerr << "failed request !" << std::endl;
                return false;
            }
            cout<<"ok:["<"] times"<1);
        }
    } catch (Exception& exc) {
        std::cerr <<"caught:"<< exc.displayText() << std::endl;
        return false;
    }

    return true;
}
  1. 下面是POST请求代码:
bool HttpSender::send(void)
{
    Poco::URI purl(g_plateform_serverURL);
    HTTPClientSession session(purl.getHost(),g_plateform_port);//指定目标的IP地址和端口

    //指定请求的类型,这个类的构造函数还有其他默认参数,例如第二个默认的是目标在服务的路劲,第三个是 HTTP消息的版本
    char buffer[1024] = {0};//定义一个接受数组
    HTTPResponse response;//定义一个回应类对象
    Poco::JSON::Parser parser;
    string ret;
    HTTPRequest request(HTTPRequest::HTTP_POST, purl.getPath(), Poco::Net::HTTPMessage::HTTP_1_1);
    request.setContentType("application/x-www-form-urlencoded");//设置HTTP消息的内容编码类型
    request.setContentLength( (int)m_arg.length() );//长度

    std::ostream& send = session.sendRequest(request);//sendRequest(...)返回一个输出流的引用,用法见下句
    send << m_arg <//发送出去


    try
    {
        std::istream& res = session.receiveResponse(response);//receiveResponse(...)返回一个输入流的引用,用法见下句
        res.read(buffer,1024);//读,输入
        //上句接收网站回应的信息 返回输入流 并copy到标准控制台输出流
    }
    catch(Poco::Net::MessageException& e)
    {
        std::cout<<"MessageException-> post wrong-: " << e.displayText();
        LOG.warning(e.displayText());
        session.reset();
        return false;
    }
    catch(Poco::Net::NetException& e)
    {
        std::cout<<"NetException: " << e.displayText();
        LOG.warning(e.displayText());
        session.reset();
        return false;
    }
    catch(Poco::Exception& e)
    {
        std::cout<<"Poco Exception:" << e.displayText();
        LOG.warning(e.displayText());
        session.reset();
        return false;
    }

    try
    {
        Poco::JSON::Query query( parser.parse( buffer ) );
        ret = query.findValue("result", "none");
    }
    catch (Poco::JSON::JSONException& e)
    {
        cout<<"JSONException"<return false;
    }

    if(  ret != "OK" )
    {
        return false;
    }else
    {
        return true;
    }
}

你可能感兴趣的:(POCO学习)