POCO GitHub地址https://github.com/pocoproject/poco
http_example.cpp
#include <iostream> #include <Poco/URI.h> #include <Poco/StreamCopier.h> #include <Poco/Net/HTTPClientSession.h> #include <Poco/Net/HTTPRequest.h> #include <Poco/Net/HTMLForm.h> #include <Poco/Net/HTTPResponse.h> #include <Poco/Net/NetException.h> using namespace Poco; using namespace Poco::Net; void getExample() { try { URI uri("http://api.eyekey.com/face/Check/checking?app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=http%3A%2F%2Fpicview01.baomihua.com%2Fphotos%2F20120713%2Fm_14_634778197959062500_40614445.jpg"); HTTPClientSession session(uri.getHost(), uri.getPort()); HTTPRequest request(HTTPRequest::HTTP_GET, uri.getPathAndQuery()); session.sendRequest(request); HTTPResponse response; std::istream &is = session.receiveResponse(response); const HTTPResponse::HTTPStatus &status = response.getStatus(); if (HTTPResponse::HTTPStatus::HTTP_OK == status) { std::string result; StreamCopier::copyToString(is, result); std::cout << result << std::endl; } else std::cout << status << std::endl; } catch (const NetException &ex) { std::cerr << "ex: " << ex.displayText() << std::endl; } } void postExample() { try { URI uri("http://api.eyekey.com/face/Check/checking"); HTTPClientSession session(uri.getHost(), uri.getPort()); HTMLForm form; form.add("app_id", "f89ae61fd63d4a63842277e9144a6bd2"); form.add("app_key", "af1cd33549c54b27ae24aeb041865da2"); form.add("url", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535455294084&di=8ca75c2b8817900345c35dbe0d1940a5&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F50%2F67%2F01300533963892135071679109235.png"); HTTPRequest request(HTTPRequest::HTTP_POST, uri.getPath(), HTTPRequest::HTTP_1_1); form.prepareSubmit(request); form.write(session.sendRequest(request)); HTTPResponse response; std::istream &is = session.receiveResponse(response); const HTTPResponse::HTTPStatus &status = response.getStatus(); if (HTTPResponse::HTTPStatus::HTTP_OK == status) { std::string result; StreamCopier::copyToString(is, result); std::cout << result << std::endl; } else std::cout << status << std::endl; } catch (const NetException &ex) { std::cerr << "ex: " << ex.displayText() << std::endl; } } int main() { getExample(); std::cout << std::endl; postExample(); std::cout << std::endl; std::system("pause"); return 0; }
https_example.cpp
#include <iostream> #include <Poco/URI.h> #include <Poco/StreamCopier.h> #include <Poco/Net/SSLManager.h> #include <Poco/Net/AcceptCertificateHandler.h> #include <Poco/Net/Context.h> #include <Poco/Net/HTTPSClientSession.h> #include <Poco/Net/HTTPRequest.h> #include <Poco/Net/HTMLForm.h> #include <Poco/Net/HTTPResponse.h> #include <Poco/Net/NetException.h> using namespace std; using namespace Poco; using namespace Poco::Net; void getExample() { try { SSLManager::InvalidCertificateHandlerPtr handlerPtr(new AcceptCertificateHandler(false)); Context::Ptr context = new Context(Context::CLIENT_USE, ""); SSLManager::instance().initializeClient(nullptr, handlerPtr, context); HTTPSClientSession session(context); URI uri("https://api.eyekey.com/face/Check/checking?app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=http%3A%2F%2Fpicview01.baomihua.com%2Fphotos%2F20120713%2Fm_14_634778197959062500_40614445.jpg"); session.setHost(uri.getHost()); session.setPort(uri.getPort()); HTTPRequest request(HTTPRequest::HTTP_GET, uri.getPathAndQuery()); session.sendRequest(request); HTTPResponse response; istream &is = session.receiveResponse(response); const HTTPResponse::HTTPStatus &status = response.getStatus(); if (HTTPResponse::HTTPStatus::HTTP_OK == status) { StreamCopier::copyStream(is, cout); cout << endl; } else cout << status << endl; } catch (const NetException &ex) { cerr << ex.displayText() << endl; } } void postExample() { try { SSLManager::InvalidCertificateHandlerPtr handlerPtr(new AcceptCertificateHandler(false)); Context::Ptr context = new Context(Context::CLIENT_USE, ""); SSLManager::instance().initializeClient(nullptr, handlerPtr, context); HTTPSClientSession session(context); URI uri("https://api.eyekey.com/face/Check/checking"); session.setHost(uri.getHost()); session.setPort(uri.getPort()); HTTPRequest request(HTTPRequest::HTTP_POST, uri.getPath()); HTMLForm form; form.set("app_id", "f89ae61fd63d4a63842277e9144a6bd2"); form.set("app_key", "af1cd33549c54b27ae24aeb041865da2"); form.set("url", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535455294084&di=8ca75c2b8817900345c35dbe0d1940a5&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F50%2F67%2F01300533963892135071679109235.png"); form.prepareSubmit(request); form.write(session.sendRequest(request)); HTTPResponse response; istream &is = session.receiveResponse(response); const HTTPResponse::HTTPStatus &status = response.getStatus(); if (HTTPResponse::HTTPStatus::HTTP_OK == status) { string result; StreamCopier::copyToString(is, result); cout << result << endl; } else cout << status << endl; } catch (const NetException &ex) { cerr << ex.displayText() << endl; } } int main() { getExample(); cout << endl; postExample(); cout << endl; system("pause"); return 0; }
原文地址:https://www.cnblogs.com/buyishi/p/9519778.html
时间: 2024-11-12 09:16:28