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.

96 lines
2.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: httpcpost.c,v 1.2 2004/08/26 17:02:24 rans Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C (example)
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #include <nanohttp/nanohttp-client.h>
  25. #ifdef WIN32
  26. #include <stdafx.h>
  27. #include <malloc.h>
  28. #else
  29. #include <stdio.h>
  30. #endif
  31. #ifdef WIN32
  32. int _tmain(int argc, _TCHAR* argv[])
  33. #else
  34. int main(int argc, char *argv[])
  35. #endif
  36. {
  37. httpc_conn_t *conn;
  38. hresponse_t *res;
  39. hpair_t *pair;
  40. char postdata[1054];
  41. int status;
  42. if (argc < 2) {
  43. fprintf(stderr, "usage %s <url> \n", argv[0]);
  44. exit(1);
  45. }
  46. log_set_level(HLOG_VERBOSE);
  47. conn = httpc_new();
  48. log_debug1("Opening connection");
  49. status = httpc_post_open(conn, argv[1]);
  50. if (status) {
  51. log_error2("Can not open connection (%d)", status);
  52. return 1;
  53. }
  54. while (1) {
  55. printf("Enter chunk (max 1024): ");
  56. gets(postdata);
  57. if (!strcmp(postdata, ".")) break;
  58. log_debug1("sending chunk ...");
  59. httpc_post_send(conn, postdata, strlen(postdata));
  60. }
  61. log_debug1("please wait ...");
  62. res = httpc_post_finish(conn);
  63. if (res != NULL) {
  64. log_info2("Spec : '%s'", res->spec);
  65. log_info2("Status: %d", res->errcode);
  66. log_info2("Desc : '%s'", res->desc);
  67. if (res->body) {
  68. pair = res->header;
  69. while (pair != NULL) {
  70. log_debug3("%s: %s", pair->key, pair->value);
  71. pair = pair->next;
  72. }
  73. puts(res->body);
  74. }
  75. else
  76. log_error1("body is null");
  77. hresponse_free(res);
  78. } else {
  79. log_error1("response object is null");
  80. }
  81. httpc_free(conn);
  82. return 0;
  83. }