[ 登录 ] - [ 注册 ] | 免费代理IP 数据无忧_无忧代理IP_DATA5U最新上线 | 代码示例DEMO | IP测试视频 | 用户协议
C++代码调用示例


#include 
#include 

size_t writeCallback(char* ptr, size_t size, size_t nmemb, void* userdata) {
  ((std::string*)userdata)->append((char*)ptr, size * nmemb);
  return size * nmemb;
}

int main() {

  CURL* curl = curl_easy_init();

  // 获取代理IP
  CURLcode res; 
  std::string proxyIpPort;
  curl_easy_setopt(curl, CURLOPT_URL, "http://api.ip.data5u.com/dynamic/get.html?order=你的提取码&sep=3");
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &proxyIpPort);
  res = curl_easy_perform(curl);

  // 设置代理
  std::string ip = proxyIpPort.substr(0, proxyIpPort.find(':'));
  std::string port = proxyIpPort.substr(proxyIpPort.find(':')+1);
  curl_easy_setopt(curl, CURLOPT_PROXY, ip.c_str());
  curl_easy_setopt(curl, CURLOPT_PROXYPORT, std::stoi(port));

  // 获取目标网页 
  std::string html;
  curl_easy_setopt(curl, CURLOPT_URL, "http://www.bing.com");
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
  res = curl_easy_perform(curl);

  std::cout << html << std::endl;

  curl_easy_cleanup(curl);
}


微信公众号
关注微信公众号