testminiwget.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* $Id: testminiwget.c,v 1.5 2016/01/24 17:24:36 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2005-2016 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution.
  7. * */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "miniwget.h"
  11. /**
  12. * This program uses the miniwget / miniwget_getaddr function
  13. * from miniwget.c in order to retreive a web ressource using
  14. * a GET HTTP method, and store it in a file.
  15. */
  16. int main(int argc, char * * argv)
  17. {
  18. void * data;
  19. int size, writtensize;
  20. FILE *f;
  21. char addr[64];
  22. int status_code = -1;
  23. if(argc < 3) {
  24. fprintf(stderr, "Usage:\t%s url file\n", argv[0]);
  25. fprintf(stderr, "Example:\t%s http://www.google.com/ out.html\n", argv[0]);
  26. return 1;
  27. }
  28. data = miniwget_getaddr(argv[1], &size, addr, sizeof(addr), 0, &status_code);
  29. if(!data || (status_code != 200)) {
  30. if(data) free(data);
  31. fprintf(stderr, "Error %d fetching %s\n", status_code, argv[1]);
  32. return 1;
  33. }
  34. printf("local address : %s\n", addr);
  35. printf("got %d bytes\n", size);
  36. f = fopen(argv[2], "wb");
  37. if(!f) {
  38. fprintf(stderr, "Cannot open file %s for writing\n", argv[2]);
  39. free(data);
  40. return 1;
  41. }
  42. writtensize = fwrite(data, 1, size, f);
  43. if(writtensize != size) {
  44. fprintf(stderr, "Could only write %d bytes out of %d to %s\n",
  45. writtensize, size, argv[2]);
  46. } else {
  47. printf("%d bytes written to %s\n", writtensize, argv[2]);
  48. }
  49. fclose(f);
  50. free(data);
  51. return 0;
  52. }