HTTP客户端类

HTTPClient provides low-level access to HTTP communication. For a higher-level interface, you may want to take a look at HTTPRequest first, which has a tutorial available here.

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

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

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

  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 was 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. OS.delay_msec(500)
  14. assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
  15. # Some headers
  16. var headers = [
  17. "User-Agent: Pirulo/1.0 (Godot)",
  18. "Accept: */*"
  19. ]
  20. err = http.request(HTTPClient.METHOD_GET, "/ChangeLog-5.php", headers) # Request a page from the site (this one was chunked..)
  21. assert(err == OK) # Make sure all is OK.
  22. while http.get_status() == HTTPClient.STATUS_REQUESTING:
  23. # Keep polling for as long as the request is being processed.
  24. http.poll()
  25. print("Requesting...")
  26. if not OS.has_feature("web"):
  27. OS.delay_msec(500)
  28. else:
  29. # Synchronous HTTP requests are not supported on the web,
  30. # so wait for the next main loop iteration.
  31. yield(Engine.get_main_loop(), "idle_frame")
  32. assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
  33. print("response? ", http.has_response()) # Site might not have a response.
  34. if http.has_response():
  35. # If there is a response...
  36. headers = http.get_response_headers_as_dictionary() # Get response headers.
  37. print("code: ", http.get_response_code()) # Show response code.
  38. print("**headers:\\n", headers) # Show headers.
  39. # Getting the HTTP Body
  40. if http.is_response_chunked():
  41. # Does it use chunks?
  42. print("Response is Chunked!")
  43. else:
  44. # Or just plain Content-Length
  45. var bl = http.get_response_body_length()
  46. print("Response Length: ", bl)
  47. # This method works for both anyway
  48. var rb = PoolByteArray() # Array that will hold the data.
  49. while http.get_status() == HTTPClient.STATUS_BODY:
  50. # While there is body left to be read
  51. http.poll()
  52. var chunk = http.read_response_body_chunk() # Get a chunk.
  53. if chunk.size() == 0:
  54. # Got nothing, wait for buffers to fill a bit.
  55. OS.delay_usec(1000)
  56. else:
  57. rb = rb + chunk # Append to read buffer.
  58. # Done!
  59. print("bytes got: ", rb.size())
  60. var text = rb.get_string_from_ascii()
  61. print("Text: ", text)
  62. quit()