http_client_class.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. .. _doc_http_client_class:
  2. HTTP client class
  3. =================
  4. :ref:`HTTPClient <class_HTTPClient>` provides low-level access to HTTP communication.
  5. For a more higher-level interface, you may want to take a look at :ref:`HTTPRequest <class_HTTPRequest>` first,
  6. which has a tutorial available :ref:`here <doc_http_request_class>`.
  7. Here's an example of using the :ref:`HTTPClient <class_HTTPClient>`
  8. class. It's just a script, so it can be run by executing:
  9. ::
  10. c:\godot> godot -s http_test.gd
  11. It will connect and fetch a website.
  12. ::
  13. extends SceneTree
  14. # HTTPClient demo
  15. # This simple class can do HTTP requests, it will not block but it needs to be polled
  16. func _init():
  17. var err = 0
  18. var http = HTTPClient.new() # Create the Client
  19. err = http.connect_to_host("www.php.net",80) # Connect to host/port
  20. assert(err == OK) # Make sure connection was OK
  21. # Wait until resolved and connected
  22. while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
  23. http.poll()
  24. print("Connecting..")
  25. OS.delay_msec(500)
  26. assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
  27. # Some headers
  28. var headers = [
  29. "User-Agent: Pirulo/1.0 (Godot)",
  30. "Accept: */*"
  31. ]
  32. err = http.request(HTTPClient.METHOD_GET, "/ChangeLog-5.php", headers) # Request a page from the site (this one was chunked..)
  33. assert(err == OK) # Make sure all is OK
  34. while http.get_status() == HTTPClient.STATUS_REQUESTING:
  35. # Keep polling until the request is going on
  36. http.poll()
  37. print("Requesting..")
  38. OS.delay_msec(500)
  39. assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
  40. print("response? ", http.has_response()) # Site might not have a response.
  41. if http.has_response():
  42. # If there is a response..
  43. headers = http.get_response_headers_as_dictionary() # Get response headers
  44. print("code: ", http.get_response_code()) # Show response code
  45. print("**headers:\\n", headers) # Show headers
  46. # Getting the HTTP Body
  47. if http.is_response_chunked():
  48. # Does it use chunks?
  49. print("Response is Chunked!")
  50. else:
  51. # Or just plain Content-Length
  52. var bl = http.get_response_body_length()
  53. print("Response Length: ",bl)
  54. # This method works for both anyway
  55. var rb = PoolByteArray() # Array that will hold the data
  56. while http.get_status() == HTTPClient.STATUS_BODY:
  57. # While there is body left to be read
  58. http.poll()
  59. var chunk = http.read_response_body_chunk() # Get a chunk
  60. if chunk.size() == 0:
  61. # Got nothing, wait for buffers to fill a bit
  62. OS.delay_usec(1000)
  63. else:
  64. rb = rb + chunk # Append to read buffer
  65. # Done!
  66. print("bytes got: ", rb.size())
  67. var text = rb.get_string_from_ascii()
  68. print("Text: ", text)
  69. quit()