You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9KB

  1. #include "testcurl.h"
  2. testcurl::testcurl( wxWindow* parent )
  3. :
  4. testcurlGUI( parent )
  5. {
  6. texte_URL->SetFocus ();
  7. texte_URL->SetSelection (-1, -1);
  8. curlhandle = curl_easy_init ();
  9. curl_easy_setopt (curlhandle, CURLOPT_WRITEFUNCTION, WriteDataCallback);
  10. curl_easy_setopt (curlhandle, CURLOPT_WRITEDATA, (void*)&curldata);
  11. }
  12. testcurl::~testcurl()
  13. {
  14. curl_easy_cleanup (curlhandle);
  15. curl_global_cleanup ();
  16. }
  17. void testcurl::OnGo( wxCommandEvent& event )
  18. {
  19. if (texte_URL->IsEmpty())
  20. wxMessageBox("URL vide", "Erreur", wxOK|wxICON_EXCLAMATION);
  21. else
  22. {
  23. texte_Resultat->SetValue("");
  24. wxString url = texte_URL->GetValue();
  25. curl_easy_setopt (curlhandle, CURLOPT_URL, _C(url));
  26. printf ("url = %s\n", _C(url));
  27. if (checkbox_Proxy->IsChecked())
  28. {
  29. wxString proxy = texte_Proxy->GetValue();
  30. curl_easy_setopt (curlhandle, CURLOPT_PROXY, _C(proxy));
  31. curl_easy_setopt (curlhandle, CURLOPT_PROXYPORT, spin_Proxy->GetValue());
  32. printf ("proxy = %s:%i\n", _C(proxy), spin_Proxy->GetValue());
  33. }
  34. else
  35. curl_easy_setopt (curlhandle, CURLOPT_PROXY, "");
  36. curldata.content = (char*) malloc(1);
  37. curldata.size = 0;
  38. if (curlhandle)
  39. curl_easy_perform(curlhandle);
  40. printf ("Content = %s", curldata.content);
  41. wxString content = wxString::FromAscii(curldata.content);
  42. texte_Resultat->SetValue(content);
  43. wxMessageBox("Fini !", "Fini !", wxOK);
  44. if (curldata.content)
  45. free (curldata.content);
  46. }
  47. }
  48. static size_t WriteDataCallback(void *contents, size_t size, size_t nmemb, void *userp)
  49. {
  50. size_t realsize = size * nmemb;
  51. CurlData *mem = (CurlData *)userp;
  52. mem->content = (char*) realloc(mem->content, mem->size + realsize + 1);
  53. if (mem->content == NULL) {
  54. // out of memory!
  55. printf("not enough memory (realloc returned NULL)\n");
  56. exit(EXIT_FAILURE);
  57. }
  58. memcpy(&(mem->content[mem->size]), contents, realsize);
  59. mem->size += realsize;
  60. mem->content[mem->size] = 0;
  61. return realsize;
  62. }