libcurl的编译在其它博客里已经介绍了,这里不再讲解。
#include<iostream>
#include<string>
#include<curl\curl.h>
using namespace std;
//get请求和post请求数据响应函数
size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
{
//在注释的里面可以打印请求流,cookie的信息
//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)
{
//curl初始化
CURL *curl = curl_easy_init();
// curl返回值
CURLcode res;
if (curl)
{
//设置curl的请求头
struct curl_slist* header_list = NULL;
header_list = curl_slist_append(header_list, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
//不接收响应头数据0代表不接收 1代表接收
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
//设置请求的URL地址
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
//设置ssl验证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
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_CONNECTTIMEOUT, 6); // set transport and time out time
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 6);
// 开启请求
res = curl_easy_perform(curl);
}
// 释放curl
curl_easy_cleanup(curl);
return res;
}
int main()
{
string getUrlStr = "https://www.baidu.com/";
string getResponseStr;
auto res = curl_get_req(getUrlStr, getResponseStr);
if (res == CURLE_OK)
{
cout << getResponseStr << endl;
}
getchar();
return 0;
}
注意使用curl接收的数据字符串编码格式为utf8编码的,在vs2015中控制台上对于中文是显示不正常,需要进项编码转换,utf8转换为ansi格式。