Posted By

bcmoney on 12/10/10


Tagged

curl http proxy c++ libcurl curlpp


Versions (?)

proxy.cpp


 / Published in: C++
 

URL: http://bcmoney-mobiletv.com/blog/2009/05/01/the-server-side-proxy/

Simple library cpp-netlib: http://cpp-netlib.github.com/

CURL wrapper: http://curlpp.org/index.php/examples/

Other resources:

http://msdn.microsoft.com/en-us/library/dd323349(VS.85).aspx#wsw.httpclient.cpp

http://www.codeproject.com/KB/IP/CppHttpWebForm.aspx

http://read.pudn.com/downloads/sourcecode/windows/internet/624/Proxy.cpp__.htm

http://cpp-netlib.svn.sourceforge.net/viewvc/cpp-netlib/trunk/libs/network/example/http_client.cpp?view=markup

http://projectivemotion.com/news/using-libcurl-with-c-simple-oop-static-class-exampleusando-libcurl-en-c-ejemplo-de-oop

http://www.learncpp.com/cpp-tutorial/713-command-line-arguments/

  1. #include <iostream>
  2. #include "curl/curl.h"
  3.  
  4. /*
  5.  * This is a very simple example of how to use libcurl + curlpp from within
  6.  * a C++ program to achieve a server-side web proxy.
  7.  *
  8.  * USAGE:
  9.  * MyCurl::get("http://example.com");
  10.  *
  11.  * @author Bryan Copeland
  12.  * @author Amado Martinez
  13.  * @author Todd Papaioannou
  14.  */
  15.  
  16.  
  17. class MyCurl
  18. {
  19. private:
  20.  
  21. public:
  22. // Write any errors in here
  23.  
  24. static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
  25. {
  26.  
  27. // Is there anything in the buffer?
  28. if (buffer_in != NULL)
  29. {
  30. // Append the data to the buffer
  31. buffer_in->append(data, size * nmemb);
  32.  
  33. // How much did we write?
  34. return size * nmemb;
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40. static std::string get(const char* url)
  41. {
  42. CURL *curl;
  43. CURLcode result;
  44.  
  45. // Create our curl handle
  46. curl = curl_easy_init();
  47.  
  48. char errorBuffer[CURL_ERROR_SIZE];
  49. // Write all expected data in here
  50. std::string buffer;
  51.  
  52. if (curl)
  53. {
  54. // Now set up all of the curl options
  55. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
  56. curl_easy_setopt(curl, CURLOPT_URL, url);
  57. curl_easy_setopt(curl, CURLOPT_HEADER, 0);
  58. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  59. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
  60. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, MyCurl::writer);
  61. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  62.  
  63. // Attempt to retrieve the remote page
  64. result = curl_easy_perform(curl);
  65.  
  66. // Always cleanup
  67. curl_easy_cleanup(curl);
  68. }
  69.  
  70. if(result == CURLE_OK)
  71. return buffer;
  72. return std::string();
  73. }
  74. };

Report this snippet  

You need to login to post a comment.