http_client_class.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = PackedByteArray() # 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. using Godot;
  93. public partial class HTTPTest : SceneTree
  94. {
  95. // HTTPClient demo.
  96. // This simple class can make HTTP requests; it will not block, but it needs to be polled.
  97. public override async void _Initialize()
  98. {
  99. Error err;
  100. HTTPClient http = new HTTPClient(); // Create the client.
  101. err = http.ConnectToHost("www.php.net", 80); // Connect to host/port.
  102. Debug.Assert(err == Error.Ok); // Make sure the connection is OK.
  103. // Wait until resolved and connected.
  104. while (http.GetStatus() == HTTPClient.Status.Connecting || http.GetStatus() == HTTPClient.Status.Resolving)
  105. {
  106. http.Poll();
  107. GD.Print("Connecting...");
  108. OS.DelayMsec(500);
  109. }
  110. Debug.Assert(http.GetStatus() == HTTPClient.Status.Connected); // Check if the connection was made successfully.
  111. // Some headers.
  112. string[] headers = { "User-Agent: Pirulo/1.0 (Godot)", "Accept: */*" };
  113. err = http.Request(HTTPClient.Method.Get, "/ChangeLog-5.php", headers); // Request a page from the site.
  114. Debug.Assert(err == Error.Ok); // Make sure all is OK.
  115. // Keep polling for as long as the request is being processed.
  116. while (http.GetStatus() == HTTPClient.Status.Requesting)
  117. {
  118. http.Poll();
  119. GD.Print("Requesting...");
  120. if (OS.HasFeature("web"))
  121. {
  122. // Synchronous HTTP requests are not supported on the web,
  123. // so wait for the next main loop iteration.
  124. await ToSignal(Engine.GetMainLoop(), "idle_frame");
  125. }
  126. else
  127. {
  128. OS.DelayMsec(500);
  129. }
  130. }
  131. Debug.Assert(http.GetStatus() == HTTPClient.Status.Body || http.GetStatus() == HTTPClient.Status.Connected); // Make sure the request finished well.
  132. GD.Print("Response? ", http.HasResponse()); // The site might not have a response.
  133. // If there is a response...
  134. if (http.HasResponse())
  135. {
  136. headers = http.GetResponseHeaders(); // Get response headers.
  137. GD.Print("Code: ", http.GetResponseCode()); // Show response code.
  138. GD.Print("Headers:");
  139. foreach (string header in headers)
  140. {
  141. // Show headers.
  142. GD.Print(header);
  143. }
  144. if (http.IsResponseChunked())
  145. {
  146. // Does it use chunks?
  147. GD.Print("Response is Chunked!");
  148. }
  149. else
  150. {
  151. // Or just Content-Length.
  152. GD.Print("Response Length: ", http.GetResponseBodyLength());
  153. }
  154. // This method works for both anyways.
  155. List<byte> rb = new List<byte>(); // List that will hold the data.
  156. // While there is data left to be read...
  157. while (http.GetStatus() == HTTPClient.Status.Body)
  158. {
  159. http.Poll();
  160. byte[] chunk = http.ReadResponseBodyChunk(); // Read a chunk.
  161. if (chunk.Length == 0)
  162. {
  163. // If nothing was read, wait for the buffer to fill.
  164. OS.DelayMsec(500);
  165. }
  166. else
  167. {
  168. // Append the chunk to the read buffer.
  169. rb.AddRange(chunk);
  170. }
  171. }
  172. // Done!
  173. GD.Print("Bytes Downloaded: ", rb.Count);
  174. string text = Encoding.ASCII.GetString(rb.ToArray());
  175. GD.Print(text);
  176. }
  177. Quit();
  178. }
  179. }