curl http libcurl 功能使用

心已赠人 2022-03-16 10:08 376阅读 0赞
  1. /*
  2. * This example shows a HTTP PUT operation. PUTs a file given as a command
  3. * line argument to the URL also given on the command line.
  4. *
  5. * This example also uses its own read callback.
  6. *
  7. * Here's an article on how to setup a PUT handler for Apache:
  8. * http://www.apacheweek.com/features/put
  9. */
  10. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  11. {
  12. size_t retcode;
  13. curl_off_t nread;
  14. /* in real-world cases, this would probably get this data differently
  15. as this fread() stuff is exactly what the library already would do
  16. by default internally */
  17. retcode = fread(ptr, size, nmemb, (FILE*)stream);
  18. nread = (curl_off_t)retcode;
  19. fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  20. " bytes from file\n", nread);
  21. return retcode;
  22. }
  23. //http协议上传
  24. void fileUploadHttp()
  25. {
  26. CURL *curl;
  27. CURLcode res;
  28. FILE * hd_src;
  29. struct stat file_info;
  30. QString locale = ui->lineEdit_path->text();
  31. char *file = locale.toLatin1().data();
  32. QFileInfo fileInfo(locale);
  33. QString fileName = fileInfo.fileName();
  34. QString remote = "http://www.example.com/upload.php?filename=" + fileName;
  35. char *remote_url = remote.toLatin1().data(); //服务器路径
  36. /* get the file size of the local file */
  37. stat(file, &file_info);
  38. /* get a FILE * of the same file, could also be made with
  39. fdopen() from the previous descriptor, but hey this is just
  40. an example! */
  41. hd_src = fopen(file, "rb");
  42. /* In windows, this will init the winsock stuff */
  43. curl_global_init(CURL_GLOBAL_ALL);
  44. /* get a curl handle */
  45. curl = curl_easy_init();
  46. if(curl) {
  47. /* we want to use our own read function */
  48. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  49. /* enable uploading */
  50. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  51. /* HTTP PUT please */
  52. curl_easy_setopt(curl, CURLOPT_PUT, 1L);
  53. /* specify target URL, and note that this URL should include a file
  54. name, not only a directory */
  55. curl_easy_setopt(curl, CURLOPT_URL, remote_url);
  56. /* now specify which file to upload */
  57. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  58. /* provide the size of the upload, we specicially typecast the value
  59. to curl_off_t since we must be sure to use the correct data size */
  60. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  61. (curl_off_t)file_info.st_size);
  62. /* Now run off and do what you've been told! */
  63. res = curl_easy_perform(curl);
  64. /* Check for errors */
  65. if(res != CURLE_OK)
  66. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  67. curl_easy_strerror(res));
  68. /* always cleanup */
  69. curl_easy_cleanup(curl);
  70. }
  71. fclose(hd_src); /* close the local file */
  72. curl_global_cleanup();
  73. }

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读