client.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. client.c - Client using the GET method to retrieve a web page.
  3. This sample demonstrates retrieving content using the HTTP GET method.
  4. Copyright (c) All Rights Reserved. See copyright notice at the bottom of the file.
  5. */
  6. /***************************** Includes *******************************/
  7. #include "appweb.h"
  8. /********************************* Code *******************************/
  9. MAIN(simpleClient, int argc, char **argv, char **envp)
  10. {
  11. Http *http;
  12. HttpStream *conn;
  13. char *err;
  14. int status;
  15. /*
  16. Create the Multithreaded Portable Runtime and start it.
  17. */
  18. mprCreate(argc, argv, 0);
  19. mprStart();
  20. /*
  21. Get a client http object to work with. We can issue multiple requests with this one object.
  22. Add the conn as a root object so the GC won't collect it while we are using it.
  23. */
  24. http = httpCreate(HTTP_CLIENT_SIDE);
  25. /*
  26. Open a connection to issue the GET. Then finalize the request output - this forces the request out.
  27. */
  28. if ((conn = httpRequest("GET", "http://example.com/index.html", NULL, 1, &err)) == 0) {
  29. mprError("Can't get URL: %s", err);
  30. exit(2);
  31. }
  32. status = httpGetStatus(conn);
  33. /*
  34. Examine the HTTP response HTTP code. 200 is success.
  35. */
  36. if (status != 200) {
  37. mprError("Server responded with status %d\n", status);
  38. exit(1);
  39. }
  40. mprPrintf("Server responded with: %s\n", httpReadString(conn));
  41. mprDestroy();
  42. return 0;
  43. }
  44. /*
  45. Copyright (c) Embedthis Software. All Rights Reserved.
  46. This software is distributed under commercial and open source licenses.
  47. You may use the Embedthis Open Source license or you may acquire a
  48. commercial license from Embedthis Software. You agree to be fully bound
  49. by the terms of either license. Consult the LICENSE.md distributed with
  50. this software for full details and other copyrights.
  51. */