zoukankan      html  css  js  c++  java
  • Poco C++——HTTP的post请求和get请求

      两种请求都需要包含头文件:

      #include <iostream>

      #include <string>

      #include "Poco/Net/HTTPClientSession.h"

      #include "Poco/Net/HTTPRequest.h"

      #include "Poco/Net/HTTPResponse.h"

      #include "Poco/StreamCopier.h"

      #include "Poco/Net/NetException.h"

      #include "Poco/Net/HTMLForm.h"

      #include "Poco/URI.h"

    一、get请求

     try {
                URI url("https://www.baidu.com");
                HTTPClientSession session(url.getHost(),url.getPort());
                HTTPRequest req(HTTPRequest::HTTP_GET,HTTPRequest::HTTP_1_1);
                req.set("/s?word=", searhName);
                session.sendRequest(req);
                HTTPResponse res;
                istream & is = session.receiveResponse(res);
                StreamCopier::copyToString(is, result);
            }
            catch(NetException & ex){
                result = ex.displayText();
            }

    二、post请求

     1 try{
     2       URI url("http://test.xheart.cn/xheartdevelop05/user/login/validate");
     3     HTTPClientSession session(url.getHost(),url.getPort());
     4     HTTPRequest request(HTTPRequest::HTTP_POST,url.getPath(),HTTPRequest::HTTP_1_1);
     5     HTMLForm form;
     6     form.add("userName", "1220002");
     7     form.add("password", "1234567");
     8     form.prepareSubmit(request);
     9     form.write(session.sendRequest(request));
    10     HTTPResponse res;
    11     std::istream & is = session.receiveResponse(res);
    12     StreamCopier::copyStream(is, std::cout);
    13       
    14 }
    15  catch(NetException & ex){
    16     std::cout << ex.displayText();          
    17 }
  • 相关阅读:
    HDOJ 2102 A计划(bfs)
    HDOJ 1226 超级密码(bfs)
    第一周——Photoshop软件的发展史,并说明其优缺点。
    第一周——Mobile Apps (手机应用)分析
    POJ 3090
    HDU 2824
    UVA 10673
    POJ 1061
    HDU 1358
    POJ 2406
  • 原文地址:https://www.cnblogs.com/mupiaomiao/p/4723597.html
Copyright © 2011-2022 走看看