mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-03 05:48:42 +03:00
Merge pull request #2139 from corrigentia/patch-117
Tweak http_client_class
This commit is contained in:
@@ -4,7 +4,7 @@ HTTP client class
|
||||
=================
|
||||
|
||||
:ref:`HTTPClient <class_HTTPClient>` provides low-level access to HTTP communication.
|
||||
For a more higher-level interface, you may want to take a look at :ref:`HTTPRequest <class_HTTPRequest>` first,
|
||||
For a higher-level interface, you may want to take a look at :ref:`HTTPRequest <class_HTTPRequest>` first,
|
||||
which has a tutorial available :ref:`here <doc_http_request_class>`.
|
||||
|
||||
Here's an example of using the :ref:`HTTPClient <class_HTTPClient>`
|
||||
@@ -21,19 +21,19 @@ It will connect and fetch a website.
|
||||
extends SceneTree
|
||||
|
||||
# HTTPClient demo
|
||||
# This simple class can do HTTP requests, it will not block but it needs to be polled
|
||||
# This simple class can do HTTP requests; it will not block, but it needs to be polled.
|
||||
|
||||
func _init():
|
||||
var err = 0
|
||||
var http = HTTPClient.new() # Create the Client
|
||||
var http = HTTPClient.new() # Create the Client.
|
||||
|
||||
err = http.connect_to_host("www.php.net",80) # Connect to host/port
|
||||
assert(err == OK) # Make sure connection was OK
|
||||
err = http.connect_to_host("www.php.net", 80) # Connect to host/port.
|
||||
assert(err == OK) # Make sure connection was OK.
|
||||
|
||||
# Wait until resolved and connected
|
||||
# Wait until resolved and connected.
|
||||
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
|
||||
http.poll()
|
||||
print("Connecting..")
|
||||
print("Connecting...")
|
||||
OS.delay_msec(500)
|
||||
|
||||
assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
|
||||
@@ -45,12 +45,12 @@ It will connect and fetch a website.
|
||||
]
|
||||
|
||||
err = http.request(HTTPClient.METHOD_GET, "/ChangeLog-5.php", headers) # Request a page from the site (this one was chunked..)
|
||||
assert(err == OK) # Make sure all is OK
|
||||
assert(err == OK) # Make sure all is OK.
|
||||
|
||||
while http.get_status() == HTTPClient.STATUS_REQUESTING:
|
||||
# Keep polling until the request is going on
|
||||
# Keep polling for as long as the request is being processed.
|
||||
http.poll()
|
||||
print("Requesting..")
|
||||
print("Requesting...")
|
||||
OS.delay_msec(500)
|
||||
|
||||
assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
|
||||
@@ -58,11 +58,11 @@ It will connect and fetch a website.
|
||||
print("response? ", http.has_response()) # Site might not have a response.
|
||||
|
||||
if http.has_response():
|
||||
# If there is a response..
|
||||
# If there is a response...
|
||||
|
||||
headers = http.get_response_headers_as_dictionary() # Get response headers
|
||||
print("code: ", http.get_response_code()) # Show response code
|
||||
print("**headers:\\n", headers) # Show headers
|
||||
headers = http.get_response_headers_as_dictionary() # Get response headers.
|
||||
print("code: ", http.get_response_code()) # Show response code.
|
||||
print("**headers:\\n", headers) # Show headers.
|
||||
|
||||
# Getting the HTTP Body
|
||||
|
||||
@@ -76,17 +76,17 @@ It will connect and fetch a website.
|
||||
|
||||
# This method works for both anyway
|
||||
|
||||
var rb = PoolByteArray() # Array that will hold the data
|
||||
var rb = PoolByteArray() # Array that will hold the data.
|
||||
|
||||
while http.get_status() == HTTPClient.STATUS_BODY:
|
||||
# While there is body left to be read
|
||||
http.poll()
|
||||
var chunk = http.read_response_body_chunk() # Get a chunk
|
||||
var chunk = http.read_response_body_chunk() # Get a chunk.
|
||||
if chunk.size() == 0:
|
||||
# Got nothing, wait for buffers to fill a bit
|
||||
# Got nothing, wait for buffers to fill a bit.
|
||||
OS.delay_usec(1000)
|
||||
else:
|
||||
rb = rb + chunk # Append to read buffer
|
||||
rb = rb + chunk # Append to read buffer.
|
||||
|
||||
# Done!
|
||||
|
||||
|
||||
Reference in New Issue
Block a user