C + + to implement an HTTP network connection, the need for third-party libraries, libcurl use is still very convenient
Environment: Win32 + VS2015
If you want to use it under Linux, it's basically the same
1, download compile Libcurl
Download the curl source, find the VS project, follow the x86 x64 and compile the static lib with the corresponding debug and release
2, building the project
1) Curl header file and Lib Copy to project directory
2) Configure the Lib directory in the Include and additional library directories Libcurl in the additional include directory Libcurl
3) Add pre-compiled macros Use_openssl and Curl_staticlib
4) Add as dependent libraries
Crypt32.lib
Ws2_32.lib
Wldap32.lib
Libcurl.lib
Note the version corresponds
3, code example
[CPP]View PlainCopyprint?
- #include <iostream>
- #include <string>
- #include "curl/curl.h"
- Using namespace std;
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Wldap32.lib")
- #pragma comment (lib, "Libcurl.lib")
- Reply of the Requery
- size_t req_reply (void *ptr, size_t size, size_t nmemb, void *stream)
- {
- cout << "----->reply" << Endl;
- String *str = (string*) stream;
- cout << *str << Endl;
- (*STR). Append ((char*) ptr, size*nmemb);
- return size * NMEMB;
- }
- HTTP GET
- Curlcode curl_get_req (const std::string &url, std::string &response)
- {
- //Init Curl
- CURL *curl = Curl_easy_init ();
- //RES code
- Curlcode Res;
- if (Curl)
- {
- //Set params
- Curl_easy_setopt (Curl, Curlopt_url, url.c_str ()); //URL
- Curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, false); //If want to use HTTPS
- Curl_easy_setopt (Curl, Curlopt_ssl_verifyhost, false); //Set peer and host verify false
- Curl_easy_setopt (Curl, curlopt_verbose, 1);
- Curl_easy_setopt (Curl, curlopt_readfunction, NULL);
- Curl_easy_setopt (Curl, curlopt_writefunction, req_reply);
- Curl_easy_setopt (Curl, Curlopt_writedata, (void *) &response);
- Curl_easy_setopt (Curl, curlopt_nosignal, 1);
- Curl_easy_setopt (Curl, Curlopt_header, 1);
- Curl_easy_setopt (Curl, curlopt_connecttimeout, 3); //Set transport and time out time
- Curl_easy_setopt (Curl, curlopt_timeout, 3);
- //Start req
- res = curl_easy_perform (curl);
- }
- //Release Curl
- Curl_easy_cleanup (curl);
- return res;
- }
- HTTP POST
- Curlcode curl_post_req (const string &url, const string &postparams, String &response)
- {
- //Init Curl
- CURL *curl = Curl_easy_init ();
- //RES code
- Curlcode Res;
- if (Curl)
- {
- //Set params
- Curl_easy_setopt (Curl, curlopt_post, 1); //Post req
- Curl_easy_setopt (Curl, Curlopt_url, url.c_str ()); //URL
- Curl_easy_setopt (Curl, Curlopt_postfields, postparams.c_str ()); //params
- Curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, false); //If want to use HTTPS
- Curl_easy_setopt (Curl, Curlopt_ssl_verifyhost, false); //Set peer and host verify false
- Curl_easy_setopt (Curl, curlopt_verbose, 1);
- Curl_easy_setopt (Curl, curlopt_readfunction, NULL);
- Curl_easy_setopt (Curl, curlopt_writefunction, req_reply);
- Curl_easy_setopt (Curl, Curlopt_writedata, (void *) &response);
- Curl_easy_setopt (Curl, curlopt_nosignal, 1);
- Curl_easy_setopt (Curl, Curlopt_header, 1);
- Curl_easy_setopt (Curl, curlopt_connecttimeout, 3);
- Curl_easy_setopt (Curl, curlopt_timeout, 3);
- //Start req
- res = curl_easy_perform (curl);
- }
- //Release Curl
- Curl_easy_cleanup (curl);
- return res;
- }
- int main ()
- {
- //Global init
- Curl_global_init (Curl_global_all);
- //Test get Requery
- String geturlstr = "Http://cn.bing.com/images/trending?form=Z9LH";
- String Getresponsestr;
- Auto res = Curl_get_req (geturlstr, GETRESPONSESTR);
- if (res! = CURLE_OK)
- Cerr << "Curl_easy_perform () Failed:" + string (Curl_easy_strerror (res)) << Endl;
- Else
- cout << getresponsestr << Endl;
- //test post Requery
- String posturlstr = "https://www.baidu.com/s";
- String postparams = "F=8&rsv_bp=1&rsv_idx=1&word=picture&tn=98633779_hao_pg";
- String Postresponsestr;
- Auto res = Curl_post_req (posturlstr, Postparams, POSTRESPONSESTR);
- if (res! = CURLE_OK)
- Cerr << "Curl_easy_perform () Failed:" + string (Curl_easy_strerror (res)) << Endl;
- Else
- cout << postresponsestr << Endl;
- //Global release
- Curl_global_cleanup ();
- System ("pause");
- return 0;
- }
- Get and post can be used to request HTML information, or you can request XML and JSON strings
- Custom header fields and cookies can be added
- This is the simple interface of Libcurl, basically the same as the blocking test request, Libcurl has the higher order asynchronous concurrency interface, the use more complex
http://blog.csdn.net/u012234115/article/details/71371962
C + + uses the Libcurl library to send HTTP requests (get and post can be used to request HTML information or to request XML and JSON strings)