Advertisement
hungcuong21891

Test CURL chuẩn

Nov 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /*
  2. * HTTP POST with authentiction using "basic" method.
  3. * Hybrid of anyauthput.c and postinmemory.c
  4. *
  5. * Usage:
  6. * cc basicauthpost.c -lcurl -o basicauthpost
  7. * ./basicauthpost
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <curl/curl.h>
  14.  
  15. struct MemoryStruct {
  16. char *memory;
  17. size_t size;
  18. };
  19.  
  20. static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *context) {
  21. size_t bytec = size * nmemb;
  22. struct MemoryStruct *mem = (struct MemoryStruct *)context;
  23. mem->memory = (char*)realloc(mem->memory, mem->size + bytec + 1);
  24. if (mem->memory == NULL) {
  25. printf("not enough memory (realloc returned NULL)\n");
  26. return 0;
  27. }
  28. memcpy(&(mem->memory[mem->size]), ptr, bytec);
  29. mem->size += bytec;
  30. mem->memory[mem->size] = 0;
  31. return nmemb;
  32. }
  33.  
  34. int main(void) {
  35. CURL *curl;
  36. CURLcode res;
  37. struct MemoryStruct chunk;
  38. chunk.memory = (char*)malloc(1);
  39. chunk.size = 0;
  40. chunk.memory[chunk.size] = 0;
  41. curl_global_init(CURL_GLOBAL_ALL);
  42. curl = curl_easy_init();
  43. if (curl) {
  44. curl_easy_setopt(curl, CURLOPT_URL, "https://api.leoem.com/api/card");
  45. char *val1 = curl_easy_escape(curl, "tricky & ugly", 0);
  46. char *val2 = curl_easy_escape(curl, "Hello from cURL!!!", 0);
  47. char *fields = (char*)malloc(4 + strlen(val1) + 1 + 4 + strlen(val2) + 1);
  48. size_t fieldSize = 4 + strlen(val1) + 1 + 4 + strlen(val2) + 1;
  49. sprintf_s(fields, fieldSize, "foo=%s&bar=%s", val1, val2);
  50. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields);
  51. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
  52. curl_easy_setopt(curl, CURLOPT_USERNAME, "f69a23d1a3b4446c8093cd8684c64f2d");
  53. curl_easy_setopt(curl, CURLOPT_PASSWORD, "2e001bbfd58443ba9072c512fda0f374");
  54. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  55. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  56. // The output from the example URL is easier to read as plain text.
  57. struct curl_slist *headers = NULL;
  58. headers = curl_slist_append(headers, "Accept: text/plain");
  59. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  60. // Make the example URL work even if your CA bundle is missing.
  61. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  62. res = curl_easy_perform(curl);
  63. if (res != CURLE_OK) {
  64. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  65. curl_easy_strerror(res));
  66. }
  67. else {
  68. printf("%s\n", chunk.memory);
  69. }
  70. // Remember to call the appropriate "free" functions.
  71. free(fields);
  72. curl_free(val1);
  73. curl_free(val2);
  74. curl_slist_free_all(headers);
  75. curl_easy_cleanup(curl);
  76. free(chunk.memory);
  77. curl_global_cleanup();
  78. }
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement