HTTPRequest¶
继承¶
Node
简要描述¶
能够发送HTTP(S)请求的节点。
描述¶
能够发送HTTP请求的节点。内部使用的是HTTPClient
可用于发出HTTP请求,即通过HTTP下载或上传文件或网页内容。
使用REST API并打印其返回字段之一的示例:
func _ready():
# 创建一个HTTP请求节点并连接其完成信号。
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", self, "_http_request_completed")
# 执行HTTP请求。下面的URL请求会返回一些JSON文本。
var error = http_request.request("https://httpbin.org/get")
if error != OK:
push_error("An error occurred in the HTTP request.")
# 当HTTP请求完成后会被调用
func _http_request_completed(result, response_code, headers, body):
var response = parse_json(body.get_string_from_utf8())
# 将打印HTTPRequest节点使用的用户代理字符串(由httpbin.org识别)。
print(response.headers["User-Agent"])
使用HTTPRequest加载和显示图像的示例:
func _ready():
# 创建一个HTTP请求节点并连接其完成信号。
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", self, "_http_request_completed")
# 执行HTTP请求。请求下面的URL会返回一张png图片
var error = http_request.request("https://via.placeholder.com/512")
if error != OK:
push_error("An error occurred in the HTTP request.")
# 当HTTP请求完成后会被调用。
func _http_request_completed(result, response_code, headers, body):
var image = Image.new()
var error = image.load_png_from_buffer(body)
if error != OK:
push_error("Couldn't load the image.")
var texture = ImageTexture.new()
texture.create_from_image(image)
# 在TextureRect节点中显示请求到的图片.
var texture_rect = TextureRect.new()
add_child(texture_rect)
texture_rect.texture = texture
成员¶
类型 | 属性名 | 默认值 |
---|---|---|
int | body_size_limit | -1 |
int | download_chunk_size | 4096 |
String | download_file | "" |
int | max_redirects | 8 |
int | timeout | 0 |
bool | use_threads | false |
方法¶
返回值类型 | 方法名称 |
---|---|
void | cancel_request() |
int | get_body_size() const |
int | get_downloaded_bytes() const |
int | get_http_client_status() const |
int | request(url: String, custom_headers: PoolStringArray = PoolStringArray( ), ssl_validate_domain: bool = true, method: int = 0, request_data: String = "") |
信号¶
- request_completed
请求完成时发出。
枚举¶
enum Result: - RESULT_SUCCESS = 0
请求成功。
-
RESULT_CHUNKED_BODY_SIZE_MISMATCH = 1
-
RESULT_CANT_CONNECT = 2
连接时请求失败。
- RESULT_CANT_RESOLVE = 3
请求解析失败。
- RESULT_CONNECTION_ERROR = 4
由于连接(读/写)错误,请求失败。
- RESULT_SSL_HANDSHAKE_ERROR = 5
SSL握手请求失败。
- RESULT_NO_RESPONSE = 6
要求尚未回应。
- RESULT_BODY_SIZE_LIMIT_EXCEEDED = 7
请求超出了其最大大小限制,请参阅body_size_limit。
- RESULT_REQUEST_FAILED = 8
请求失败(当前未使用)。
- RESULT_DOWNLOAD_FILE_CANT_OPEN = 9
HTTPRequest无法打开下载文件。
- RESULT_DOWNLOAD_FILE_WRITE_ERROR = 10
HTTPRequest无法写入下载文件。
- RESULT_REDIRECT_LIMIT_REACHED = 11
请求达到其最大重定向限制,请参阅max_redirects。
- RESULT_TIMEOUT = 12
常量¶
成员说明¶
- int body_size_limit
Default | -1 |
---|---|
setter | set_body_size_limit(value) |
getter | get_body_size_limit |
- int download_chunk_size
Default | 4096 |
---|---|
setter | set_download_chunk_size(value) |
getter | get_download_chunk_size |
- String download_file
Default | "" |
---|---|
setter | set_download_file(value) |
getter | get_download_file |
- int max_redirects
Default | 8 |
---|---|
setter | set_max_redirects(value) |
getter | get_max_redirects |
- int timeout
Default | 0 |
---|---|
setter | set_timeout(value) |
getter | get_timeout |
- bool use_threads
Default | false |
---|---|
setter | set_use_threads(value) |
getter | is_using_threads |
方法说明¶
- cancel_request cancel_request()
取消当前请求。
- get_body_size get_body_size() const
返回响应主体的长度。
**注意:**某些Web服务器可能无法发送正文长度。 在这种情况下,返回的值将是-1
。 如果使用分块传输编码,则正文长度也将为-1
。
- get_downloaded_bytes get_downloaded_bytes() const
返回此HTTPRequest下载的字节数。
- get_http_client_status get_http_client_status() const
返回基础HTTPClient的当前状态。参见HTTPClient.Status。
- request request(url: String, custom_headers: PoolStringArray = PoolStringArray( ), ssl_validate_domain: bool = true, method: int = 0, request_data: String = "")
在基础HTTPClient上创建请求。如果没有配置错误,它将尝试使用HTTPClient.connect_to_host进行连接,并将参数传递到HTTPClient.request上。
如果成功创建请求,则返回OK。 (不表示服务器已响应),如果不在树中则为ERR_UNCONFIGURED,如果仍在处理先前的请求,则为ERR_BUSY,如果给定的字符串不是有效的URL格式,则为ERR_INVALID_PARAMETER,如果不使用线程,并且HTTPClient无法连接到主机则为ERR_CANT_CONNECT 。