帮助文档>代码示例 > C语言代码示例

C语言代码示例

发布时间:2022-07-03 00:12
  1. #include
  2. #include
  3. #include
  4. #include "curl/curl.h"
  5. using namespace std;
  6. static size_t WriteMemoryCallback(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((const char*)ptr, nsize);
  11. return nsize;
  12. }
  13. void Proxy(string& resp) {
  14. CURL* curl = curl_easy_init();
  15. CURLcode res;
  16. if (curl) {
  17. curl_easy_setopt(curl, CURLOPT_URL, "https://myip.ipip.net");
  18. curl_easy_setopt(curl, CURLOPT_PROXY, "http://219.151.125.106:31615");
  19. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "895314XY:24D6YB309ZCB");
  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. {
  31. string response;
  32. Proxy(response);
  33. cout << response << endl;
  34. return 0;
  35. }
本文导读