C语言运行HTTP代码示例

  1. #include
  2. #include
  3. #include
  4. #include"curl/curl.h"
  5. usingnamespace std;
  6. staticsize_tWriteMemoryCallback(void* ptr,size_t size,size_t nmemb,void* stream){
  7. size_t nsize = size * nmemb;
  8. string* strdata =(string*)stream;
  9. if(strdata)
  10. strdata->append((constchar*)ptr, nsize);
  11. return nsize;
  12. }
  13. voidProxy(string& resp){
  14. CURL* curl = curl_easy_init();
  15. CURLcode res;
  16. if(curl){
  17. curl_easy_setopt(curl, CURLOPT_URL,"https://ip.cn/api/index?ip=&type=0");
  18. curl_easy_setopt(curl, CURLOPT_PROXY,"http://代理IP:端口号");
  19. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,"请改成您的Key:请改成您的AuthPwd");
  20. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,WriteMemoryCallback);
  21. curl_easy_setopt(curl, CURLOPT_WRITEDATA,&resp);
  22. res = curl_easy_perform(curl);
  23. curl_easy_cleanup(curl);
  24. if(res != CURLE_OK){
  25. fprintf(stderr,"request failed: %s\n", curl_easy_strerror(res));
  26. }
  27. }
  28. }
  29. int main(){
  30. string response;
  31. Proxy(response);
  32. cout << response << endl;
  33. return0;
  34. }
  35. 嵌入即可使用HTTP隧道
  36. C语言运行HTTP代码示例_第1张图片

 

你可能感兴趣的:(c语言,http,c++)