HTTP客户端类

HTTPClient 提供了对HTTP通信的低层次访问.对于一个更高级的接口,你可能需要看看 HTTPRequest ,它有一个可用的教程 here.

这是使用 HTTPClient 类的示例. 它只是一个脚本,因此它可以通过执行以下命令来运行:

GDScript

C#

  1. c:\godot> godot -s http_test.gd
  1. c:\godot> godot -s HTTPTest.cs

它将连接并获取一个网站.

GDScript

C#

  1. extends SceneTree
  2. # HTTPClient demo
  3. # This simple class can do HTTP requests; it will not block, but it needs to be polled.
  4. func _init():
  5. var err = 0
  6. var http = HTTPClient.new() # Create the Client.
  7. err = http.connect_to_host("www.php.net", 80) # Connect to host/port.
  8. assert(err == OK) # Make sure connection is OK.
  9. # Wait until resolved and connected.
  10. while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
  11. http.poll()
  12. print("Connecting...")
  13. if not OS.has_feature("web"):
  14. OS.delay_msec(500)
  15. else:
  16. yield(Engine.get_main_loop(), "idle_frame")
  17. assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Check if the connection was made successfully.
  18. # Some headers
  19. var headers = [
  20. "User-Agent: Pirulo/1.0 (Godot)",
  21. "Accept: */*"
  22. ]
  23. err = http.request(HTTPClient.METHOD_GET, "/ChangeLog-5.php", headers) # Request a page from the site (this one was chunked..)
  24. assert(err == OK) # Make sure all is OK.
  25. while http.get_status() == HTTPClient.STATUS_REQUESTING:
  26. # Keep polling for as long as the request is being processed.
  27. http.poll()
  28. print("Requesting...")
  29. if OS.has_feature("web"):
  30. # Synchronous HTTP requests are not supported on the web,
  31. # so wait for the next main loop iteration.
  32. yield(Engine.get_main_loop(), "idle_frame")
  33. else:
  34. OS.delay_msec(500)
  35. assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
  36. print("response? ", http.has_response()) # Site might not have a response.
  37. if http.has_response():
  38. # If there is a response...
  39. headers = http.get_response_headers_as_dictionary() # Get response headers.
  40. print("code: ", http.get_response_code()) # Show response code.
  41. print("**headers:\\n", headers) # Show headers.
  42. # Getting the HTTP Body
  43. if http.is_response_chunked():
  44. # Does it use chunks?
  45. print("Response is Chunked!")
  46. else:
  47. # Or just plain Content-Length
  48. var bl = http.get_response_body_length()
  49. print("Response Length: ", bl)
  50. # This method works for both anyway
  51. var rb = PoolByteArray() # Array that will hold the data.
  52. while http.get_status() == HTTPClient.STATUS_BODY:
  53. # While there is body left to be read
  54. http.poll()
  55. # Get a chunk.
  56. var chunk = http.read_response_body_chunk()
  57. if chunk.size() == 0:
  58. if not OS.has_feature("web"):
  59. # Got nothing, wait for buffers to fill a bit.
  60. OS.delay_usec(1000)
  61. else:
  62. yield(Engine.get_main_loop(), "idle_frame")
  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()
  1. class HTTPTest : SceneTree
  2. {
  3. // HTTPClient demo.
  4. // This simple class can make HTTP requests; it will not block, but it needs to be polled.
  5. public override async void _Initialize()
  6. {
  7. Error err;
  8. HTTPClient http = new HTTPClient(); // Create the client.
  9. err = http.ConnectToHost("www.php.net", 80); // Connect to host/port.
  10. Debug.Assert(err == Error.Ok); // Make sure the connection is OK.
  11. // Wait until resolved and connected.
  12. while (http.GetStatus() == HTTPClient.Status.Connecting || http.GetStatus() == HTTPClient.Status.Resolving)
  13. {
  14. http.Poll();
  15. GD.Print("Connecting...");
  16. OS.DelayMsec(500);
  17. }
  18. Debug.Assert(http.GetStatus() == HTTPClient.Status.Connected); // Check if the connection was made successfully.
  19. // Some headers.
  20. string[] headers = { "User-Agent: Pirulo/1.0 (Godot)", "Accept: */*" };
  21. err = http.Request(HTTPClient.Method.Get, "/ChangeLog-5.php", headers); // Request a page from the site.
  22. Debug.Assert(err == Error.Ok); // Make sure all is OK.
  23. // Keep polling for as long as the request is being processed.
  24. while (http.GetStatus() == HTTPClient.Status.Requesting)
  25. {
  26. http.Poll();
  27. GD.Print("Requesting...");
  28. if (OS.HasFeature("web"))
  29. {
  30. // Synchronous HTTP requests are not supported on the web,
  31. // so wait for the next main loop iteration.
  32. await ToSignal(Engine.GetMainLoop(), "idle_frame");
  33. }
  34. else
  35. {
  36. OS.DelayMsec(500);
  37. }
  38. }
  39. Debug.Assert(http.GetStatus() == HTTPClient.Status.Body || http.GetStatus() == HTTPClient.Status.Connected); // Make sure the request finished well.
  40. GD.Print("Response? ", http.HasResponse()); // The site might not have a response.
  41. // If there is a response...
  42. if (http.HasResponse())
  43. {
  44. headers = http.GetResponseHeaders(); // Get response headers.
  45. GD.Print("Code: ", http.GetResponseCode()); // Show response code.
  46. GD.Print("Headers:");
  47. foreach (string header in headers)
  48. {
  49. // Show headers.
  50. GD.Print(header);
  51. }
  52. if (http.IsResponseChunked())
  53. {
  54. // Does it use chunks?
  55. GD.Print("Response is Chunked!");
  56. }
  57. else
  58. {
  59. // Or just Content-Length.
  60. GD.Print("Response Length: ", http.GetResponseBodyLength());
  61. }
  62. // This method works for both anyways.
  63. List<byte> rb = new List<byte>(); // List that will hold the data.
  64. // While there is data left to be read...
  65. while (http.GetStatus() == HTTPClient.Status.Body)
  66. {
  67. http.Poll();
  68. byte[] chunk = http.ReadResponseBodyChunk(); // Read a chunk.
  69. if (chunk.Length == 0)
  70. {
  71. // If nothing was read, wait for the buffer to fill.
  72. OS.DelayMsec(500);
  73. }
  74. else
  75. {
  76. // Append the chunk to the read buffer.
  77. rb.AddRange(chunk);
  78. }
  79. }
  80. // Done!
  81. GD.Print("Bytes Downloaded: ", rb.Count);
  82. string text = Encoding.ASCII.GetString(rb.ToArray());
  83. GD.Print(text);
  84. }
  85. Quit();
  86. }
  87. }