Http/https Client Source Sample

Source: Internet
Author: User

Http/https Client Source Sample

Environment: zlib-1.2.8 openssl-1.0.1g curl-7.36

Author: Kagula

lastupdatedate: 2014-04

Read the Prerequisites: Basic use and configuration of CMake tools OPENSSL-1.0.1G development environment

Compiling the Zlib library

Download zlib-1.2.8.tar.gz and unzip to "D:\SDK\zlib-1.2.8", use the CMake tool to generate the Zlib.sln, open and compile in visual Studio2013.

Compiling curl-7.36.0

Assuming that open SSL is already installed to "D:\SDK\openssl-1.0.1g", set the following environment variables first

openssl_libraries=d:\sdk\openssl-1.0.1g\out32

Openssl_root_dir=d:\sdk\openssl-1.0.1g

Download Curl-7.36.0.zip from http://curl.haxx.se/and unzip to "D:\SDK\curl-7.36.0" launch CMake tool configure, set lib_eay_release separately And the ssl_eay_release variable is "D:\SDK\openssl-1.0.1g\out32\libeay32.lib", "D:\SDK\openssl-1.0.1g\out32\ Ssleay32.lib ", generate the SLn file after opening, add"use_manual"macro definition for the Curl project inside, then build the 4 projects inside the success.

Add the link library libcurl_imp.lib to the project, copy the Libcurl.dll file to the C + + project path, or the program will run without prompting for a dynamic-link library.

The following is an example of the Http/https client

Source.cpp How to invoke an example

#include <iostream>using namespace std; #include "HttpClient.h"//The following is the test code using namespace Kagula::network;void Testhttpclient () {chttpclient client;string url;string post;string cookie= "D:\\temp\\temp.txt"; string response;url = " Http://www.mydrivers.com "; the client. Get (URL, response); cout << response << Endl; Client. Post (url,post,cookie,response); cout << response << endl;url = "https://www.google.com.hk/"; client. GetS (URL, response, NULL); cout << response << endl;client. PostS (URL, post, cookie, response, NULL); cout << response << Endl;} int main (int argc, char *argv[]) {testhttpclient (); return 0;}


HttpClient.h Packaged header File

HttpClient.h Source Code Listing #ifndef _httpclient_h_#define _httpclient_h_#include <string>/* title: From http/ Get page test environment in HTTPS server: Windows 8.1Visual Studio sp1libcurl 7.36.0 Description: Go from csdn to a page, make minor changes to the source code, add a cookie support reference: Curl_eay _setopt manual http://www.helplib.net/s/linux.die/65_2740/man-3-curl-easy-setopt.shtml "C + + cout high-order format operation" http:/ Www.cnblogs.com/devymex/archive/2010/09/06/1818754.html*/namespace kagula{namespace Network{class CHttpClient{ Public:chttpclient (void); ~chttpclient (void);p ublic:/*** @brief HTTP POST request * @param strurl input parameter, requested URL address, e.g. http:// www.baidu.com* @param strpost input parameters, use the following format para1=val1¶2=val2&...* @param strcookie input parameters, cookie file names, such as D:\temp\ cookie.txt* if the cookie.* @param strresponse output parameter is not enabled, the returned contents * @return returns whether Post succeeded */int Post (const ST D::string & strURL, const std::string & strpost, const std::string& Strcookie, std::string & Strresponse); /*** @brief HTTP GET request * @param strurl input parameter, requested URL address, such as: http://www.baidu.com* @param strresponse output parameter, contents returned *@return Returns whether Post succeeded */int Get (const std::string & strURL, std::string & strresponse),/*** @brief HTTPS POST request, no certificate version * @param strurl input parameters, requested URL address, such as: https://www.alipay.com* @param strpost input parameters, use the following format para1=val1¶2=val2&...* @param Strcookie input parameter, cookie file name, such as d:\temp\cookie.txt* if blank, do not enable cookie.* @param strresponse output parameter, return content * @ param the Pcapath input parameter, the path to the CA certificate. If the input is null, the validity of the server-side certificate is not verified. * @return Returns whether Post succeeded */int PostS (const std::string & strURL, Const std::string & strpost, const std::string& Strcookie, std::string & strresponse, const char * Pcapath = N ULL);/*** @brief HTTPS GET request, no certificate version * @param strurl input parameters, requested URL address, such as: https://www.alipay.com* @param strresponse output parameters, returned content * @param the Pcapath input parameter, the path to the CA certificate. If the input is null, the validity of the server-side certificate is not verified. * @return Returns whether Post succeeded */int GetS (const std::string & strURL, Std::string & strresponse, const char * pcapath = NULL);p ublic:void setdebug (bool bdebug);p Rivate:bool m_bdebug;bool P Rintcookies (void* curl,std::string& StroUT);};} #endif


HttpClient.cpp

HttpClient.cpp source code listing include "HttpClient.h" #include <iostream> #include <curl/curl.h> #include < iomanip> #include <sstream>namespace kagula{namespace network{chttpclient::chttpclient (void): M_bDebug ( False) {}chttpclient::~chttpclient (void) {}bool chttpclient::P rintcookies (void* curl, std::string& strout) {std: : Ostringstream ostr; Curlcode res;struct curl_slist *cookies;res = curl_easy_getinfo (Curl, curlinfo_cookielist, &cookies); if (res! = CURLE_OK) {ostr << "Curl Curl_easy_getinfo failed:" << curl_easy_strerror (res) << std::endl;strout = OS Tr.str (); return false;} const struct Curl_slist *nc = cookies;int i = 1;ostr << "Cookies, Curl knows:" << Std::endl;while (NC) {OSTR & lt;< "[" << i++ << "]:" << nc->data << std::endl;nc = Nc->next;} return true;} static int OnDebug (CURL *, Curl_infotype itype, char * pData, size_t size, void *) {if (Itype = = Curlinfo_text) {//printf ("[ Text]%s\n ", pData);} else if (itype = = curlinfo_header_in) {printf ("[header_in]%s\n", PData);} else if (Itype = = curlinfo_header_out) {printf ("[header_out]%s\n", PData);} else if (Itype = = curlinfo_data_in) {printf ("[data_in]%s\n", PData);} else if (Itype = = curlinfo_data_out) {printf ("[data_out]%s\n", PData);} return 0;} Static size_t Onwritedata (void* buffer, size_t size, size_t nmemb, void* lpvoid) {std::string* str = reinterpret_cast<st D::string*> (LPVOID); if (NULL = = STR | | NULL = = buffer) {return-1;} char* pData = reinterpret_cast<char*> (buffer); Str->append (pData, size * nmemb); return nmemb;} int chttpclient::P ost (const std::string& strurl, const std::string& strpost, const std::string& Strcookie, std::string& strresponse) {Curlcode res; curl* curl = Curl_easy_init (); if (NULL = = Curl) {return curle_failed_init;} if (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (Curl, curlopt_debugfunction, OnDebug);} Curl_easy_setopt (Curl, Curlopt_url, strurl.c_str ()); Curl_easy_setopt (Curl, Curlopt_post, 1); curl_easy_setopt (Curl, Curlopt_postfields, strpost.c_str ()); curl_easy_setopt (Curl, CURLOPT_ Readfunction, NULL); curl_easy_setopt (Curl, curlopt_writefunction, onwritedata); curl_easy_setopt (Curl, CURLOPT_ WriteData, (void *) &strresponse); curl_easy_setopt (Curl, curlopt_nosignal, 1); if (Strcookie.length () >0) {Curl_ Easy_setopt (Curl, Curlopt_cookiefile, (void *) &strcookie) curl_easy_setopt (Curl, Curlopt_cookiejar, (void *) &strcookie);} Curl_easy_setopt (Curl, curlopt_connecttimeout, 3); curl_easy_setopt (Curl, curlopt_timeout, 3); res = Curl_easy_perform (curl); Curl_easy_cleanup (curl); return res;} int Chttpclient::get (const std::string & strURL, std::string & strresponse) {Curlcode res; curl* curl = Curl_easy_init (); if (NULL = = Curl) {return curle_failed_init;} if (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (Curl, curlopt_debugfunction, OnDebug);} Curl_easy_setopt (Curl, Curlopt_url, strurl.c_str ()); curl_easy_setopt (Curl, Curlopt_readfunctiOn, NULL); curl_easy_setopt (Curl, curlopt_writefunction, onwritedata); curl_easy_setopt (Curl, Curlopt_writedata, ( void *) &strresponse)/*** When multiple threads are using time-out processing, there is a sleep or wait operation in the main thread. * If this option is not set, Libcurl will send a signal to interrupt the wait and cause the program to exit. */curl_easy_setopt (Curl, curlopt_nosignal, 1); curl_easy_setopt (Curl, curlopt_connecttimeout, 3); Curl_easy_setopt ( Curl, Curlopt_timeout, 3); res = curl_easy_perform (curl); Curl_easy_cleanup (curl); return res;}  int chttpclient::P osts (const std::string & strurl, const std::string & strpost, const std::string& Strcookie, Std::string & strresponse, const char * pcapath) {Curlcode res; curl* curl = Curl_easy_init (); if (NULL = = Curl) {return curle_failed_init;} if (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (Curl, curlopt_debugfunction, OnDebug);} Curl_easy_setopt (Curl, Curlopt_url, Strurl.c_str ()) curl_easy_setopt (Curl, curlopt_post, 1); Curl_easy_setopt (Curl, Curlopt_postfields, Strpost.c_str ()); curl_easy_setopt (Curl, curlopt_readfunction, NULL); curl_easy_setopt (Curl, curlopt_writefunction, onwritedata); curl_easy_setopt (Curl, Curlopt_writedata, (void *) &strresponse); curl_easy_setopt (Curl, curlopt_nosignal, 1); if (Strcookie.length () >0) {curl_easy_setopt (curl, Curlopt_cookiefile, (void *) &strcookie); curl_easy_setopt (Curl, Curlopt_cookiejar, (void *) &strcookie);} if (NULL = = Pcapath) {curl_easy_setopt (curl, Curlopt_ssl_verifypeer, false); Curl_easy_setopt (Curl, Curlopt_ssl_ Verifyhost, false);} The else{//default is PEM, so no setup is required, additional support is der//curl_easy_setopt (Curl,curlopt_sslcerttype, "PEM"); curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, True); Curl_easy_setopt (Curl, Curlopt_cainfo, Pcapath);} Curl_easy_setopt (Curl, curlopt_connecttimeout, 3); curl_easy_setopt (Curl, curlopt_timeout, 3); res = Curl_easy_perform (curl); Curl_easy_cleanup (curl); return res;} int chttpclient::gets (const std::string & strURL, std::string & strresponse, const char * pcapath) {Curlcode res; curl* curl = Curl_easy_init (); if (NULL = = Curl) {return curle_failed_iNIT;} if (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (Curl, curlopt_debugfunction, OnDebug);} Curl_easy_setopt (Curl, Curlopt_url, strurl.c_str ()); curl_easy_setopt (Curl, curlopt_readfunction, NULL); curl_easy_ Setopt (Curl, curlopt_writefunction, onwritedata); curl_easy_setopt (Curl, Curlopt_writedata, (void *) &strresponse ); curl_easy_setopt (Curl, curlopt_nosignal, 1); if (NULL = = Pcapath) {curl_easy_setopt (curl, Curlopt_ssl_verifypeer, FALSE); Curl_easy_setopt (curl, Curlopt_ssl_verifyhost, false);} Else{curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, true); Curl_easy_setopt (Curl, Curlopt_cainfo, Pcapath);} Curl_easy_setopt (Curl, curlopt_connecttimeout, 3); curl_easy_setopt (Curl, curlopt_timeout, 3); res = Curl_easy_perform (curl); Curl_easy_cleanup (curl); return res;} void Chttpclient :: Setdebug (bool bdebug) {m_bdebug = Bdebug;}}}

Supplementary reading materials

Uploading files to an FTP server using the Libcurl implementation

http://blog.csdn.net/lee353086/article/details/5823145

Download the Http://www.baidu.com/img/baidu.gif sample using Libcurl

Http://www.cppblog.com/qiujian5628/archive/2008/06/28/54873.html

Summary of the use of Libcurl (i)

Http://www.vimer.cn/2010/03/libcurl%E7%9A%84%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93%EF%BC%88%E4%B8%80%EF%BC%89.html

PHP's curl implements Get,post and cookies

Http://www.tsingpost.com/articles/201403/525.html

Http/https Client Source Sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.