C++要實現http網絡連接,需要借助第三方庫,libcurl使用起來還是很方便的
環境:win32 + vs2015
如果要在Linux下使用,基本同理
1,下載編譯libcurl
下載curl源碼,找到vs工程,按照x86 x64 並對應debug和release編譯出靜態庫lib
2,構建工程
1)curl頭文件和lib拷貝到工程目錄
2)配置附加包含目錄libcurl中的include和附加庫目錄libcurl中的lib目錄
3)添加預編譯宏USE_OPENSSL和CURL_STATICLIB
4)添加如依賴庫
crypt32.lib
ws2_32.lib
wldap32.lib
libcurl.lib
注意版本對應
3,代碼示例
- #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和post可以用於請求html信息,也可以請求xml和json等串
- 可以添加自定義的header 域和cookies
- 這是libcurl的簡單接口,基本等同於阻塞試請求,libcurl有高階的異步並發接口,運用更復雜
http://blog.csdn.net/u012234115/article/details/71371962