http_client_class.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 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. .. warning::
  8. When exporting to Android, make sure to enable the ``INTERNET``
  9. permission in the Android export preset before exporting the project or
  10. using one-click deploy. Otherwise, network communication of any kind will be
  11. blocked by Android.
  12. Here's an example of using the :ref:`HTTPClient <class_HTTPClient>`
  13. class. It's just a script, so it can be run by executing:
  14. .. tabs::
  15. .. code-tab:: console GDScript
  16. c:\godot> godot -s http_test.gd
  17. .. code-tab:: console C#
  18. c:\godot> godot -s HTTPTest.cs
  19. It will connect and fetch a website.
  20. .. tabs::
  21. .. code-tab:: gdscript GDScript
  22. extends SceneTree
  23. # HTTPClient demo
  24. # This simple class can do HTTP requests; it will not block, but it needs to be polled.
  25. func _init():
  26. var err = 0
  27. var http = HTTPClient.new() # Create the Client.
  28. err = http.connect_to_host("www.php.net", 80) # Connect to host/port.
  29. assert(err == OK) # Make sure connection is OK.
  30. # Wait until resolved and connected.
  31. while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
  32. http.poll()
  33. print("Connecting...")
  34. if not OS.has_feature("web"):
  35. OS.delay_msec(500)
  36. else:
  37. yield(Engine.get_main_loop(), "idle_frame")
  38. assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Check if the connection was made successfully.
  39. # Some headers
  40. var headers = [
  41. "User-Agent: Pirulo/1.0 (Godot)",
  42. "Accept: */*"
  43. ]
  44. err = http.request(HTTPClient.METHOD_GET, "/ChangeLog-5.php", headers) # Request a page from the site (this one was chunked..)
  45. assert(err == OK) # Make sure all is OK.
  46. while http.get_status() == HTTPClient.STATUS_REQUESTING:
  47. # Keep polling for as long as the request is being processed.
  48. http.poll()
  49. print("Requesting...")
  50. if OS.has_feature("web"):
  51. # Synchronous HTTP requests are not supported on the web,
  52. # so wait for the next main loop iteration.
  53. yield(Engine.get_main_loop(), "idle_frame")
  54. else:
  55. OS.delay_msec(500)
  56. assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
  57. print("response? ", http.has_response()) # Site might not have a response.
  58. if http.has_response():
  59. # If there is a response...
  60. headers = http.get_response_headers_as_dictionary() # Get response headers.
  61. print("code: ", http.get_response_code()) # Show response code.
  62. print("**headers:\\n", headers) # Show headers.
  63. # Getting the HTTP Body
  64. if http.is_response_chunked():
  65. # Does it use chunks?
  66. print("Response is Chunked!")
  67. else:
  68. # Or just plain Content-Length
  69. var bl = http.get_response_body_length()
  70. print("Response Length: ", bl)
  71. # This method works for both anyway
  72. var rb = PoolByteArray() # Array that will hold the data.
  73. while http.get_status() == HTTPClient.STATUS_BODY:
  74. # While there is body left to be read
  75. http.poll()
  76. # Get a chunk.
  77. var chunk = http.read_response_body_chunk()
  78. if chunk.size() == 0:
  79. if not OS.has_feature("web"):
  80. # Got nothing, wait for buffers to fill a bit.
  81. OS.delay_usec(1000)
  82. else:
  83. yield(Engine.get_main_loop(), "idle_frame")
  84. else:
  85. rb = rb + chunk # Append to read buffer.
  86. # Done!
  87. print("bytes got: ", rb.size())
  88. var text = rb.get_string_from_ascii()
  89. print("Text: ", text)
  90. quit()
  91. .. code-tab:: csharp
  92. class HTTPTest : SceneTree
  93. {
  94. // HTTPClient demo.
  95. // This simple class can make HTTP requests; it will not block, but it needs to be polled.
  96. public override async void _Initialize()
  97. {
  98. Error err;
  99. HTTPClient http = new HTTPClient(); // Create the client.
  100. err = http.ConnectToHost("www.php.net", 80); // Connect to host/port.
  101. Debug.Assert(err == Error.Ok); // Make sure the connection is OK.
  102. // Wait until resolved and connected.
  103. while (http.GetStatus() == HTTPClient.Status.Connecting || http.GetStatus() == HTTPClient.Status.Resolving)
  104. {
  105. http.Poll();
  106. GD.Print("Connecting...");
  107. OS.DelayMsec(500);
  108. }
  109. Debug.Assert(http.GetStatus() == HTTPClient.Status.Connected); // Check if the connection was made successfully.
  110. // Some headers.
  111. string[] headers = { "User-Agent: Pirulo/1.0 (Godot)", "Accept: */*" };
  112. err = http.Request(HTTPClient.Method.Get, "/ChangeLog-5.php", headers); // Request a page from the site.
  113. Debug.Assert(err == Error.Ok); // Make sure all is OK.
  114. // Keep polling for as long as the request is being processed.
  115. while (http.GetStatus() == HTTPClient.Status.Requesting)
  116. {
  117. http.Poll();
  118. GD.Print("Requesting...");
  119. if (OS.HasFeature("web"))
  120. {
  121. // Synchronous HTTP requests are not supported on the web,
  122. // so wait for the next main loop iteration.
  123. await ToSignal(Engine.GetMainLoop(), "idle_frame");
  124. }
  125. else
  126. {
  127. OS.DelayMsec(500);
  128. }
  129. }
  130. Debug.Assert(http.GetStatus() == HTTPClient.Status.Body || http.GetStatus() == HTTPClient.Status.Connected); // Make sure the request finished well.
  131. GD.Print("Response? ", http.HasResponse()); // The site might not have a response.
  132. // If there is a response...
  133. if (http.HasResponse())
  134. {
  135. headers = http.GetResponseHeaders(); // Get response headers.
  136. GD.Print("Code: ", http.GetResponseCode()); // Show response code.
  137. GD.Print("Headers:");
  138. foreach (string header in headers)
  139. {
  140. // Show headers.
  141. GD.Print(header);
  142. }
  143. if (http.IsResponseChunked())
  144. {
  145. // Does it use chunks?
  146. GD.Print("Response is Chunked!");
  147. }
  148. else
  149. {
  150. // Or just Content-Length.
  151. GD.Print("Response Length: ", http.GetResponseBodyLength());
  152. }
  153. // This method works for both anyways.
  154. List<byte> rb = new List<byte>(); // List that will hold the data.
  155. // While there is data left to be read...
  156. while (http.GetStatus() == HTTPClient.Status.Body)
  157. {
  158. http.Poll();
  159. byte[] chunk = http.ReadResponseBodyChunk(); // Read a chunk.
  160. if (chunk.Length == 0)
  161. {
  162. // If nothing was read, wait for the buffer to fill.
  163. OS.DelayMsec(500);
  164. }
  165. else
  166. {
  167. // Append the chunk to the read buffer.
  168. rb.AddRange(chunk);
  169. }
  170. }
  171. // Done!
  172. GD.Print("Bytes Downloaded: ", rb.Count);
  173. string text = Encoding.ASCII.GetString(rb.ToArray());
  174. GD.Print(text);
  175. }
  176. Quit();
  177. }
  178. }