http_client_class.rst 7.7 KB

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