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")
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;
CURLcode curl_get_req(
const std::string &url, std::string &response)
CURL *curl = curl_easy_init();
CURLcode res;
if (curl)
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,
false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,
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);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
CURLcode curl_post_req(
const string &url,
const string &postParams, string &response)
CURL *curl = curl_easy_init();
CURLcode res;
if (curl)
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,
false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,
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);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
int main()
curl_global_init(CURL_GLOBAL_ALL);
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;
cout << getResponseStr << endl;
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;
cout << postResponseStr << endl;
curl_global_cleanup();
system(
"pause");
return 0;