4.10. HTTPClient Class

Note: This class is not available with the PO_NO_NETWORK parse option.

The HTTPClient class can be used to communicate with HTTP servers using the HTTP or HTTPS (HTTP using an SSL/TLS encrypted connection) protocol.

By default 'Connection: Keep-Alive' is always sent regardless of the HTTP protocol level set for the object, however if a server response contains 'Connection: close', the connection will be closed as soon as the full response (including any message body if present) has been read.

HTTP redirect responses are supported and can be limited with the max_redirects constructor hash key or by using the HTTPClient::setMaxRedirects() method. The default number of redirects is 5.

HTTP basic authentication is supported; set the username and password in the URL (ex: http://username:password@host:port/path). To change the URL from the one set by the constructor, call HTTPClient::setURL().

HTTP proxies and basic proxy authentication are supported by setting the proxy constructor hash key to the proxy URL (with a proxy username and password if required) or by calling the HTTPClient::setProxyURL() method.

Objects of this class are thread-safe and support serializing multiple simultaneous requests from many threads. If a request is in progress and another thread attempts to make a request at the same time, the second thread will block until the first is complete. Therefore the total amount of time a thread could wait for a response in a multi-threaded context could be greater than the read timeout value.

This class understands and automatically decodes "deflate", "gzip", and "bzip2" content encodings as well.

The default read timeout value is 300,000 milliseconds (5 minutes). Note that the read timeout value applies to individual packets; for this reason for large transfers the overall read time could exceed the read timeout value.

When an exception is thrown (for example, a response code of < 200 or >= 400 is received from the server), any message body returned will be in the "arg" key of the exception hash.

This class understands the protocols in the following table.

Table 4.474. HTTPClient Class Protocols

Protocol

Default Port

SSL?

Description

http

80

No

Unencrypted HTTP protocol

https

443

Yes

HTTP protocol with SSL/TLS encryption


Whenever using an HTTPClient method where a hash of headers can be passed to the method, some headers are generated by default by the class and can be overridden, and some are cannot be overridden and are ignored if passed by the client. See the following tables for details.

Table 4.475. HTTPClient Mandatory Headers

Header

Description

Content-Length

This header is only sent if a message body is send, and, if so, the length is calculated automatically.


Table 4.476. HTTPClient Default, but Overridable Headers

Header

Default Value

Accept

text/html

Content-Type

text/html

User-Agent

Qore HTTP Client v0.8.0

Connection

Keep-Alive

Accept-Encoding

deflate,gzip,bzip2


This class supports posting network events to a Queue. See Event Handling for more information.

The events raised by this object are listed in the following table:

Table 4.477. HTTPClient Events

Name

Description

EVENT_HTTP_CONTENT_LENGTH

Raised when the HTTP "Content-Length" header is received.

EVENT_HTTP_CHUNKED_START

Raised when HTTP chunked data is about to be received.

EVENT_HTTP_CHUNKED_END

Raised when all HTTP chunked data has been received.

EVENT_HTTP_REDIRECT

Raised when an HTTP redirect message is received.

EVENT_HTTP_SEND_MESSAGE

Raised when an HTTP message is sent.

EVENT_HTTP_MESSAGE_RECEIVED

Raised when an HTTP message is received.

EVENT_HTTP_FOOTERS_RECEIVED

Raised when HTTP footers are received.

EVENT_HTTP_CHUNKED_DATA_RECEIVED

Raised when a block of HTTP chunked data is received.

EVENT_HTTP_CHUNK_SIZE

Raised when the next chunk size for HTTP chunked data is known.


The following table gives an overview of the methods available in the HTTPClient class.

Table 4.478. HTTPClient Class Method Overview

Method

Except?

Description

HTTPClient::constructor()

HTTPClient::constructor(hash$opts)

Y

Creates the HTTPClient object based on the parameters passed.

HTTPClient::destructor()

N

Destroys the HTTPClient object and closes any open connections.

HTTPClient::copy()

Y

Copying objects of this class is not supported, an exception will be thrown.

stringHTTPClient::getURL()

N

Returns the current URL.

nothingHTTPClient::setURL(string$url)

Y

Sets a new URL value for the next connection.

nothingHTTPClient::setUserPassword(string$user, string$pass)

nothingHTTPClient::setUserPassword()

Y

Sets or clears (in case called with no arguments) the username and password for the connection; call after HTTPClient::setURL().

nothingHTTPClient::clearUserPassword()

N

Clears the username and password values.

*stringHTTPClient::getProxyURL()

N

Returns the current proxy URL as a string or NOTHING if no proxy URL is set.

nothingHTTPClient::setProxyURL()

nothingHTTPClient::setProxyURL(string$url)

N

Sets or clears (in case called with no arguments) a new proxy URL value for the next connection.

nothingHTTPClient::clearProxyURL()

N

Clears the proxy URL value.

nothingHTTPClient::setProxyUserPassword(string$user, string$pass)

nothingHTTPClient::setProxyUserPassword()

Y

Sets or clears (in case called with no arguments) the username and password for the proxy connection; call after HTTPClient::setProxyURL().

nothingHTTPClient::clearProxyUserPassword()

N

Clears the proxy username and password values.

nothingHTTPClient::setProxySecure(softbool$secure = True)

N

Sets the SSL/TLS flag for the next connection to the proxy.

boolHTTPClient::isProxySecure()

N

Returns the SSL/TLS flag.

intHTTPClient::getMaxRedirects()

N

Returns the current max_redirects value.

nothingHTTPClient::setMaxRedirects(softint$max = 0)

N

Updates the max_redirects value.

nothingHTTPClient::connect()

N

Connects to the remote socket; SSL/TLS negotiation is performed if required.

nothingHTTPClient::disconnect()

N

Disconnects from the remote socket

*stringHTTPClient::get(string$path, hash $headers = hash(), reference$info)

*stringHTTPClient::get(string$path, hash $headers = hash())

Y

Sends an HTTP GET request and returns the message body received as a string or NOTHING if no message body is received. In order to get the headers and the body, use the HTTPClient::send() method instead. If no connection is established, an internal call to HTTPClient::connect() will be made before sending the message.

hashHTTPClient::head(string$path, hash$headers = hash(), reference$info)

hashHTTPClient::head(string$path, hash$headers = hash())

Y

Sends an HTTP HEAD request and returns as hash of the headers received. If no connection is established, an internal call to HTTPClient::connect() will be made before sending the message.

*stringHTTPClient::post(string$path, data$body, hash$headers = hash(), reference$info)

*stringHTTPClient::post(string$path, data$body, hash$headers = hash())

Y

Sends an HTTP POST request with a message body and returns the message body received as a string or NOTHING if no message body is received. In order to get the headers and the body in the response, use the HTTPClient::send() method instead. If no connection is established, an internal call to HTTPClient::connect() will be made before sending the message.

hashHTTPClient::send(data$body = binary(), string$method, string$path = "", hash $headers = hash(), bool$getbody = False, reference$info)

hashHTTPClient::send(data$body = binary(), string$method, string$path = "", hash $headers = hash(), bool$getbody = False)

Y

Sends an HTTP request with the specified method and optional message body and returns headers and any body received as a response in a hash format. If no connection is established, an internal call to HTTPClient::connect() will be made before sending the message.

nothingHTTPClient::setConnectTimeout(timeout$timeout_ms = -1)

N

Sets the connect timeout in milliseconds. Negative numbers mean use the default system connect timeout. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 30s = 30 seconds, etc.).

nothingHTTPClient::setTimeout(timeout$timeout_ms = -1)

N

Sets the default read timeout in milliseconds. Zero means immediate timeout (will return data only if it is already available), and negative numbers mean never timeout (wait forever for data). Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.).

intHTTPClient::getConnectTimeout()

N

Returns the connect timeout as an integer in milliseconds. Negative numbers mean the system default timeout is used.

intHTTPClient::getTimeout()

N

Returns the default read timeout as an integer in milliseconds. Zero means immediate timeout (only returns data if it is already available), and negative numbers mean never timeout (wait forever for data).

nothingHTTPClient::setHTTPVersion(string$ver)

Y

Sets the HTTP protocol version string for headers in outgoing messages, allowed values are '1.0' and '1.1'.

stringHTTPClient::getHTTPVersion()

N

Returns the HTTP protocol version string used in outgoing messages.

nothingHTTPClient::setSecure(softbool$secure = True)

N

Sets the object to make a secure SSL/TLS connection on the next connect if the passed argument is True, or an unencrypted cleartext connection if it is False. This method overrides the default behaviour for the protocol set for the object.

boolHTTPClient::isSecure()

N

Returns True if the current connection is encrypted, False if not.

*stringHTTPClient::verifyPeerCertificate()

N

Returns a string code giving the result of verifying the remote certificate or NOTHING if a secure connection is not established. Return value are the keys described in the X509_VerificationReasons hash constant. This hash can also be used to generate a textual description of the verification result.

*stringHTTPClient::getSSLCipherName()

N

Returns the name of the cipher as a string for an encrypted connection or NOTHING if a secure connection is not established.

*stringHTTPClient::getSSLCipherVersion()

N

Returns the version of the cipher as a string for an encrypted connection or NOTHING if a secure connection is not established.

nothingHTTPClient::setEncoding(string$encoding)

Y

Sets the string encoding for the object; any strings deserialized with this object will be tagged with this character encoding.

stringHTTPClient::getEncoding()

N

Returns the character encoding used for the object

nothingHTTPClient::setEventQueue()

nothingHTTPClient::setEventQueue(Queue$queue)

N

Sets a Queue object to receive HTTPClient and Socket events or clears the queue if no argument is passed.

boolHTTPClient::getNoDelay()

N

Returns the TCP_NODELAY setting for the object.

intHTTPClient::setNoDelay(softbool$nodelay = True)

N

Sets the TCP_NODELAY setting for the object.

boolHTTPClient::isConnected()

N

Returns True or False giving the current connection state.


4.10.1. HTTPClient::constructor()

Synopsis

Creates the HTTPClient object based on the parameters passed. To connect, call any method that requires a connection and an implicit connection is established, or call HTTPClient::connect().

Prototype

HTTPClient::constructor()

HTTPClient::constructor(hash$opts)

Example
my HTTPClient $httpclient(("url":"http://hostname:8080/path"));

Table 4.479. Arguments for HTTPClient::constructor()

Argument

Description

[hash$opts]

sets options and changes default behaviour for the object, etc. See the table below for information on valid keys and their meaning. Note that the key names are case-sensitive and therefore must all be in lower-case.


Table 4.480. HTTPClient::constructor() Option Hash Keys

Key

Description

url

A string giving the URL to connect to.

default_port

The default port number to connect to if none is given in the URL.

protocols

A hash describing new protocols, the key is the protocol name and the value is either an integer giving the default port number or a hash with 'port' and 'ssl' keys giving the default port number and a boolean value to indicate that an SSL connection should be established.

http_version

Either '1.0' or '1.1' for the claimed HTTP protocol version compliancy in outgoing message headers.

default_path

The default path to use for new connections if a path is not otherwise specified in the connection URL.

max_redirects

The maximum number of redirects before throwing an exception (the default is 5).

proxy

The proxy URL for connecting through a proxy.

timeout

The timeout value in milliseconds (also can be a relative date-time value for clarity, ex: 5m)

connect_timeout

The timeout value in milliseconds for establishing a new socket connection (also can be a relative date-time value for clarity, ex: 30s)


Table 4.481. Exceptions thrown by HTTPClient::constructor()

err

desc

HTTP-CLIENT-OPTION-ERROR

invalid or unknown option passed in option hash

HTTP-CLIENT-URL-ERROR

invalid URL string

HTTP-CLIENT-UNKNOWN-PROTOCOL

unknown protocol passed in URL


4.10.2. HTTPClient::destructor()

Synopsis

Destroys the HTTPClient object and closes any open connections.

Example
delete $httpclient;
Events

EVENT_CHANNEL_CLOSED, EVENT_DELETED

4.10.3. HTTPClient::copy()

Synopsis

Copying objects of this class is not supported, an exception will be thrown.

Table 4.482. Exceptions thrown by HTTPClient::copy()

err

desc

HTTP-CLIENT-COPY-ERROR

objects of this class may not be copied


4.10.4. HTTPClient::getURL()

Synopsis

Returns the current URL value for the object. To set the URL, use the HTTPClient::setURL() method.

Prototype

stringHTTPClient::getURL()

Example
$url = $httpclient.getURL();

Table 4.483. Return Values for HTTPClient::getURL()

Return Type

Description

string

The current URL value.


4.10.5. HTTPClient::setURL()

Synopsis

Sets the URL value for the object. To retrieve the current URL value, use the HTTPClient::getURL() method.

Prototype

nothingHTTPClient::setURL(string$url)

Example
$httpclient.setURL("https://user:password@hostname:8080/path");

Table 4.484. Arguments for HTTPClient::setURL()

Argument

Description

string$url

The new URL for the object.


Table 4.485. Exceptions thrown by HTTPClient::setURL()

err

desc

HTTP-CLIENT-URL-ERROR

invalid URL string

HTTP-CLIENT-UNKNOWN-PROTOCOL

unknown protocol passed in URL


4.10.6. HTTPClient::setUserPassword()

Synopsis

Sets or clears (in case called with no arguments) the username and password for the proxy connection; note that HTTPClient::setURL() will overwrite any values set here.

Prototype

nothingHTTPClient::setUserPassword(string$user, string$pass)

nothingHTTPClient::setUserPassword()

Example
$httpclient.setUserPassword("user1", "password1");

Table 4.486. Arguments for HTTPClient::setUserPassword()

Argument

Description

[string$user, string$pass]

The new username and password for the proxy connection; call with no arguments to clear.


4.10.7. HTTPClient::clearUserPassword()

Synopsis

Clears the proxy username and password values; normally called after HTTPClient::setURL().

Prototype

nothingHTTPClient::clearUserPassword()

Example
$httpclient.clearUserPassword();

4.10.8. HTTPClient::getProxyURL()

Synopsis

Returns the current proxy URL value for the object (NOTHING if no proxy URL is set). To set the proxy URL, use the HTTPClient::setProxyURL() method.

Prototype

*stringHTTPClient::getProxyURL()

Example
my *string $proxy_url = $httpclient.getProxyURL();

Table 4.487. Return Values for HTTPClient::getProxyURL()

Return Type

Description

*string

The current proxy URL value or NOTHING if it is not set.


4.10.9. HTTPClient::setProxyURL()

Synopsis

Sets the proxy URL value for the object; call with no argument or NOTHING to clear. To retrieve the current URL value, use the HTTPClient::getProxyURL() method.

Prototype

nothingHTTPClient::setProxyURL()

nothingHTTPClient::setProxyURL(string$url)

Example
$httpclient.setProxyURL("http://user:password@proxy_host:8080/path");

Table 4.488. Arguments for HTTPClient::setProxyURL()

Argument

Description

[string$url]

The new proxy URL for the object; call with no argument or NOTHING to clear.


Table 4.489. Exceptions thrown by HTTPClient::setProxyURL()

err

desc

HTTP-CLIENT-URL-ERROR

invalid URL string

HTTP-CLIENT-SET-PROXY-ERROR

invalid authorization credentials in proxy URL (username without password or vice-versa)

HTTP-CLIENT-PROXY-PROTOCOL-ERROR

unknown protocol passed in URL


4.10.10. HTTPClient::clearProxyURL()

Synopsis

Clears the current proxy URL

Prototype

nothingHTTPClient::clearProxyURL()

Example
$httpclient.clearProxyURL();

4.10.11. HTTPClient::setProxyUserPassword()

Synopsis

Sets or clears (in case called with no arguments) the username and password for the proxy connection; call after HTTPClient::setProxyURL() because HTTPClient::setProxyURL() will overwrite any values set here.

Prototype

nothingHTTPClient::setProxyUserPassword(string$user, string$pass)

nothingHTTPClient::setProxyUserPassword()

Example
$httpclient.setProxyUserPassword("user1", "password1");

Table 4.490. Arguments for HTTPClient::setProxyUserPassword()

Argument

Description

[string$user, string$pass]

The new username and password for the proxy connection; call with no arguments to clear.


4.10.12. HTTPClient::clearProxyUserPassword()

Synopsis

Clears the proxy username and password values; normally called after HTTPClient::setProxyURL().

Prototype

nothingHTTPClient::clearProxyUserPassword()

Example
$httpclient.clearProxyUserPassword();

4.10.13. HTTPClient::setProxySecure()

Synopsis

Sets the SSL/TLS flag for the next proxy connection. To check the flag, use the HTTPClient::isProxySecure() method.

Prototype

nothingHTTPClient::setProxySecure(softbool$secure = True)

Example
$httpclient.setProxySecure(True);

Table 4.491. Arguments for HTTPClient::setProxySecure()

Argument

Description

softbool$secure = True

sets the SSL/TLS flag for the next proxy connection


4.10.14. HTTPClient::isProxySecure()

Synopsis

Returns the SSL/TLS flag for proxy connection. To set the flag, use the HTTPClient::setProxySecure() method.

Prototype

boolHTTPClient::isProxySecure()

Example
$bool = $httpclient.isProxySecure();

Table 4.492. Return Values for HTTPClient::isProxySecure()

Return Type

Description

bool

The value of the SSL/TLS flag for the proxy connection.


4.10.15. HTTPClient::getMaxRedirects()

Synopsis

Returns the maximum number of redirects allowed for the object. To set this value, use the HTTPClient::setMaxRedirects() method.

Prototype

intHTTPClient::getMaxRedirects()

Example
$num = $httpclient.getMaxRedirects();

Table 4.493. Return Values for HTTPClient::getMaxRedirects()

Return Type

Description

int

The current value of the max_redirects setting.


4.10.16. HTTPClient::setMaxRedirects()

Synopsis

Sets the maximum number of redirects allowed for the object. To retrieve this value, use the HTTPClient::getMaxRedirects() method.

Prototype

nothingHTTPClient::setMaxRedirects(softint$max = 0)

Example
$httpclient.setMaxRedirects(0); # disable redirections

Table 4.494. Arguments for HTTPClient::setMaxRedirects()

Argument

Description

softint$max = 0

The maximum number of HTTP redirects allowed for the object before an exception is thrown.


4.10.17. HTTPClient::connect()

Synopsis

Connects to the remote socket. If the protocol indicates that a secure connection should be established (or HTTPClient::setSecure() was called previsouly), SSL/TLS negotiation will be attempted. Note: For possible exceptions, see the Socket::connect() method (or Socket::connectSSL() for secure connections).

If the TCP_NODELAY flag has been set (see HTTPClient::setNoDelay()), then after a successful connection to the remote socket, this option will be set on the socket. If an error occurs setting the TCP_NODELAY option, the internal flag is set to false (use HTTPClient::getNoDelay() to check the flag's state) and the error code can be retrieved with errno().

Prototype

nothingHTTPClient::connect()

Example
$httpclient.connect();
Events

EVENT_CONNECTING, EVENT_CONNECTED,EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED

Table 4.495. Exceptions thrown by HTTPClient::connect()

err

desc

HTTP-CLIENT-MAXIMUM-.REDIRECTS-EXCEEDED

The attempt to connect exceeeded the maximum number of redirects allowed for the object.

HTTP-CLIENT-RECEIVE-ERROR

There was an error receiving data on the socket

HTTP-CLIENT-TIMEOUT

The receive attempt timed out.


4.10.18. HTTPClient::disconnect()

Synopsis

Disconnects from the remote socket if a connection is established (otherwise does nothing).

Prototype

nothingHTTPClient::disconnect()

Example
$httpclient.disconnect();
Events

EVENT_CHANNEL_CLOSED

4.10.19. HTTPClient::get()

Synopsis

Sends an HTTP GET request and returns the message body received as a string. In order to get the headers and the body, use the HTTPClient::send() method instead. If necessary, a connection will be established via an implicit call to HTTPClient::connect() before sending the message.

If any content encoding is used, then the content is decoded and returned a string; if the content encoding uses an unknown method, then an exception is thrown.

Prototype

*stringHTTPClient::get(string$path, hash $headers = hash(), reference$info)

*stringHTTPClient::get(string$path, hash $headers = hash())

Example
my *string $html = $httpclient.get("/path/file.html");
Events

EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED, EVENT_HTTP_SEND_MESSAGE, EVENT_PACKET_SENT, EVENT_HTTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_HTTP_CONTENT_LENGTH, EVENT_HTTP_CHUNKED_START, EVENT_HTTP_CHUNKED_END, EVENT_HTTP_CHUNKED_DATA_RECEIVED, EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED, EVENT_HTTP_REDIRECT

Table 4.496. Arguments for HTTPClient::get()

Argument

Description

string$path

The path for the message (i.e. '/path/resource?method&param=value')

hash $headers = hash()

An optional hash of headers to include in the message.

[reference$info]

An optional reference to an lvalue that will be used as an output variable giving a hash of request headers and other information about the HTTP request.


Table 4.497. Return Values for HTTPClient::get()

Return Type

Description

*string

The message body returned; NOTHING in case of an erroneous reply by the server with no body.


Table 4.498. Exceptions thrown by HTTPClient::get()

err

desc

HTTP-CLIENT-TIMEOUT

timeout on response from HTTP server

HTTP-CLIENT-RECEIVE-ERROR

error communicating with HTTP server


4.10.20. HTTPClient::head()

Synopsis

Sends an HTTP HEAD request and returns the headers received. If necessary, a connection will be established via an implicit call to HTTPClient::connect() before sending the message.

Prototype

hashHTTPClient::head(string$path, hash$headers = hash(), reference$info)

hashHTTPClient::head(string$path, hash$headers = hash())

Example
$response = $httpclient.head("/path");
Events

EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED, EVENT_HTTP_SEND_MESSAGE, EVENT_PACKET_SENT, EVENT_HTTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_HTTP_CONTENT_LENGTH, EVENT_HTTP_CHUNKED_START, EVENT_HTTP_CHUNKED_END, EVENT_HTTP_CHUNKED_DATA_RECEIVED, EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED, EVENT_HTTP_REDIRECT

Table 4.499. Arguments for HTTPClient::head()

Argument

Description

string$path

The path for the message (i.e. '/path/resource?method&param=value')

hash $headers = hash()

An optional hash of headers to include in the message.

[reference$info]

An optional reference to an lvalue that will be used as an output variable giving a hash of request headers and other information about the HTTP request.


Table 4.500. Return Values for HTTPClient::head()

Return Type

Description

hash

The headers received from the HTTP server with all key names converted to lower-case.


Table 4.501. Exceptions thrown by HTTPClient::head()

err

desc

HTTP-CLIENT-TIMEOUT

timeout on response from HTTP server

HTTP-CLIENT-RECEIVE-ERROR

error communicating with HTTP server


4.10.21. HTTPClient::post()

Synopsis

Sends an HTTP POST request with a message body and returns the message body received as a response. In order to get the headers and the body, use HTTPClient::send() instead. If necessary, a connection will be established via an implicit call to HTTPClient::connect() before sending the message.

Prototype

*stringHTTPClient::post(string$path, data$body, hash$headers = hash(), reference$info)

*stringHTTPClient::post(string$path, data$body, hash$headers = hash())

Table 4.502. Arguments for HTTPClient::post()

Argument

Description

string$path

The path for the message (i.e. '/path/resource?method&param=value')

data$body

The message body to send.

hash $headers = hash()

An optional hash of headers to include in the message.

[reference$info]

An optional reference to an lvalue that will be used as an output variable giving a hash of request headers and other information about the HTTP request.


Table 4.503. Return Values for HTTPClient::post()

Return Type

Description

*string

The message body returned; NOTHING in case of an erroneous reply by the server with no body.


Table 4.504. Exceptions thrown by HTTPClient::post()

err

desc

HTTP-CLIENT-TIMEOUT

timeout on response from HTTP server

HTTP-CLIENT-RECEIVE-ERROR

error communicating with HTTP server


4.10.22. HTTPClient::send()

Synopsis

Sends an HTTP request with the specified method and optional message body and returns headers and optionally the body received as a response in a hash format. If necessary, a connection will be established via an implicit call to HTTPClient::connect() before sending the message.

Prototype

hashHTTPClient::send(data$body = binary(), string$method, string$path = "", hash $headers = hash(), bool$getbody = False, reference$info)

hashHTTPClient::send(data$body = binary(), string$method, string$path = "", hash $headers = hash(), bool$getbody = False)

Example
$msg = $httpclient.send($body, "POST", "/path", ("Connection":"Keep-Alive"));
Events

EVENT_CONNECTING, EVENT_CONNECTED, EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED, EVENT_HTTP_SEND_MESSAGE, EVENT_PACKET_SENT, EVENT_HTTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_HTTP_CONTENT_LENGTH, EVENT_HTTP_CHUNKED_START, EVENT_HTTP_CHUNKED_END, EVENT_HTTP_CHUNKED_DATA_RECEIVED, EVENT_HTTP_CHUNK_SIZE, EVENT_HTTP_FOOTERS_RECEIVED, EVENT_HTTP_REDIRECT

Table 4.505. Arguments for HTTPClient::send()

Argument

Description

data$body

The message body to send; use NOTHING to send to body.

string$method

The name of the HTTP method ('GET', 'POST', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE', or 'CONNECT').

string$path

The path for the message (i.e. '/path/resource?method&param=value')

hash $headers = hash()

An optional hash of headers to include in the message.

bool$getbody = False

If this argument is true, then the object will try to receive a message body even if no Content-Length header is present in the response. Use this only with broken servers that send messages bodies without a 'Content-Length' header.

[reference$info]

An optional reference to an lvalue that will be used as an output variable giving a hash of request headers and other information about the HTTP request.


Table 4.506. Return Values for HTTPClient::send()

Return Type

Description

hash

The headers received from the HTTP server with all key names converted to lower-case. The message body, if any, will be assigned to the value of the 'body' key.


Table 4.507. Exceptions thrown by HTTPClient::send()

err

desc

HTTP-CLIENT-TIMEOUT

timeout on response from HTTP server

HTTP-CLIENT-RECEIVE-ERROR

error communicating with HTTP server

HTTP-CLIENT-METHOD-ERROR

invalid HTTP method passed


4.10.23. HTTPClient::setConnectTimeout()

Synopsis

Sets the default connect timeout in milliseconds. Negative numbers mean use the default system timeout. Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.).

Prototype

nothingHTTPClient::setConnectTimeout(timeout$timeout_ms = -1)

Example
$httpclient.setConnectTimeout(2m); # sets timeout to 2 minutes

Table 4.508. Arguments for HTTPClient::setConnectTimeout()

Argument

Description

timeout$timeout_ms = -1

Connect timeout in milliseconds, 0 = immediate timeout (do not use), negative numbers = use system default connect timeout.


4.10.24. HTTPClient::setTimeout()

Synopsis

Sets the default read timeout in milliseconds. Zero means immediate timeout (will return data only if it is already available), and negative numbers mean never timeout (wait forever for data). Note that like all Qore functions and methods taking timeout values, a relative date/time value can be used to make the units clear (i.e. 2m = two minutes, etc.).

Prototype

nothingHTTPClient::setTimeout(timeout$timeout_ms = -1)

Example
$httpclient.setTimeout(2m); # sets timeout to 2 minutes

Table 4.509. Arguments for HTTPClient::setTimeout()

Argument

Description

timeout$timeout_ms = -1

Read timeout in milliseconds, 0 = immediate timeout, negative numbers = never timeout.


4.10.25. HTTPClient::getConnectTimeout()

Synopsis

Returns the connect timeout as an integer in milliseconds. Negative numbers mean the default system timeout is used instead.

Prototype

intHTTPClient::getConnectTimeout()

Example
$timeout = $httpclient.getConnectTimeout();

Table 4.510. Return Values for HTTPClient::getConnectTimeout()

Return Type

Description

int

The connect timeout value in milliseconds


4.10.26. HTTPClient::getTimeout()

Synopsis

Returns the default read timeout as an integer in milliseconds. Zero means immediate timeout (only returns data if it is already available), and negative numbers mean never timeout (wait forever for data).

Prototype

intHTTPClient::getTimeout()

Example
$timeout = $httpclient.getTimeout();

Table 4.511. Return Values for HTTPClient::getTimeout()

Return Type

Description

int

The read timeout value in milliseconds


4.10.27. HTTPClient::setHTTPVersion()

Synopsis

Sets the HTTP protocol version string for headers in outgoing messages, allowed values are '1.0' and '1.1'.

Prototype

nothingHTTPClient::setHTTPVersion(string$ver)

Example
$httpclient.setHTTPVersion("1.1");

Table 4.512. Arguments for HTTPClient::setHTTPVersion()

Argument

Description

string$ver

either '1.0' or '1.1' for the HTTP protocol compliance version.


Table 4.513. Exceptions thrown by HTTPClient::setHTTPVersion()

err

desc

HTTP-VERSION-ERROR

invalid HTTP version passed (allowed values: '1.0', '1.1').


4.10.28. HTTPClient::getHTTPVersion()

Synopsis

Returns the HTTP protocol version string used in outgoing messages.

Prototype

stringHTTPClient::getHTTPVersion()

Example
$version = $httpclient.getHTTPVersion();

Table 4.514. Return Values for HTTPClient::getHTTPVersion()

Return Type

Description

string

The HTTP protocol version string used in outgoing messages (either '1.0' or '1.1').


4.10.29. HTTPClient::setSecure()

Synopsis

Sets the object to make a secure SSL/TLS connection on the next connect if the passed argument is True, or an unencrypted cleartext connection if it is False. This method overrides the default behaviour for the protocol set for the object.

Note that the behavior of this method when called with no argument changed in version 0.8.0; prior to version 0.8.0 calling this method with no argument would turn off secure mode; the behavior was changed to the current functionality in order to make the usage of this method consistent with other methods of the same name and to make it more logical.

Prototype

nothingHTTPClient::setSecure(softbool$secure = True)

Example
$httpclient.setSecure(True);

Table 4.515. Arguments for HTTPClient::setSecure()

Argument

Description

softbool$secure = True

If True, a SSL/TLS connection will be attempted on the next connection. If False, an unencrypted cleartext connection will be established.


4.10.30. HTTPClient::isSecure()

Synopsis

Returns True if the current connection is encrypted, False if not.

Prototype

boolHTTPClient::isSecure()

Example
$bool = $httpclient.isSecure();

Table 4.516. Return Values for HTTPClient::isSecure()

Return Type

Description

bool

Returns True if the current connection is encrypted, False if not.


4.10.31. HTTPClient::verifyPeerCertificate()

Synopsis

Returns a string code giving the result of verifying the remote certificate or NOTHING if an encrypted connection is not established. Return value are the keys described in the X509_VerificationReasons hash constant. This hash can also be used to generate a textual description of the verification result.

Prototype

*stringHTTPClient::verifyPeerCertificate()

Example
my *string $str = $httpclient.verifyPeerCertificate();

Table 4.517. Return Values for HTTPClient::verifyPeerCertificate()

Return Type

Description

*string

A string code giving the result of verifying the peer's certificate. No value is returned if a secure connection has not been established.


4.10.32. HTTPClient::getSSLCipherName()

Synopsis

Returns the name of the cipher for an encrypted connection.

Prototype

*stringHTTPClient::getSSLCipherName()

Example
my *string $str = $httpclient.getSSLCipherName();

Table 4.518. Return Values for HTTPClient::getSSLCipherName()

Return Type

Description

*string

The name of the cipher for a secure connection. No value is returned if no secure connection has been established.


4.10.33. HTTPClient::getSSLCipherVersion()

Synopsis

Returns the version of the cipher for an encrypted connection.

Prototype

*stringHTTPClient::getSSLCipherVersion()

Example
my *string $str = $httpclient.getSSLCipherVersion();

Table 4.519. Return Values for HTTPClient::getSSLCipherVersion()

Return Type

Description

*string

The version of the cipher for a secure connection. No value is returned if no secure connection has been established.


4.10.34. HTTPClient::setEncoding()

Synopsis

Sets the string encoding for the object; any strings deserialized with this object will be tagged with this character encoding.

Prototype

nothingHTTPClient::setEncoding(string$encoding)

Example
$httpclient.setEncoding("ISO-8859-1");

Table 4.520. Arguments for HTTPClient::setEncoding()

Argument

Description

string$encoding

The string encoding to use for this object.


Table 4.521. Exceptions thrown by HTTPClient::setEncoding()

err

desc

HTTP-CLIENT-SETSTRINGENCODING-ERROR

missing encoding parameter from method call


4.10.35. HTTPClient::getEncoding()

Synopsis

Returns the character encoding used for the object

Prototype

stringHTTPClient::getEncoding()

Example
$str = $httpclient.getEncoding();

Table 4.522. Return Values for HTTPClient::getEncoding()

Return Type

Description

string

The character encoding used for the object.


4.10.36. HTTPClient::setEventQueue()

Synopsis

Sets a Queue object to receive HTTPClient and Socket events. To remove the event queue and stop monitoring events, pass NOTHING to the method. See Event Handling for more information.

Prototype

nothingHTTPClient::setEventQueue()

nothingHTTPClient::setEventQueue(Queue$queue)

Example
$httpclient.setEventQueue($queue);

Table 4.523. Arguments for HTTPClient::setEventQueue()

Argument

Description

[Queue$queue]

To set the event queue, pass a Queue object; to clear the event queue, pass no argument to the function.


4.10.37. HTTPClient::getNoDelay()

Synopsis

Returns the TCP_NODELAY setting for the HTTPClient object. See also HTTPClient::setNoDelay().

Prototype

boolHTTPClient::getNoDelay()

Example
$nodelay = $httpclient.getNoDelay();

Table 4.524. Return Value for HTTPClient::getNoDelay()

Return Type

Description

bool

The TCP_NODELAY setting for the HTTPClient object.


4.10.38. HTTPClient::setNoDelay()

Synopsis

Sets the TCP_NODELAY setting for the HTTPClient object; when this setting is True, then data will be immediately sent out over the HTTPClient object's socket, when it is False, then data transmission may be delayed to be packaged with other data for the same target.

Delayed data transmissions may cause problems when the sender immediately closes the socket after sending data; in this case the receiver may not get the data even though the send succeeded.

Note that if no value is given to the method, the argument will be assumed to be True, and output buffering will be turned off for the HTTPClient object.

If the socket is not connected when this call is made, then an internal flag is set and the TCP_NODELAY option is enabled when the next connection is established. If the socket is connected, then if an error occurs setting the TCP_NODELAY option on the socket, this method will return a non-zero error code; the actual error can be checked with the errno() function.

See also HTTPClient::getNoDelay().

Prototype

intHTTPClient::setNoDelay(softbool$nodelay = True)

Example
$httpclient.setNoDelay(True);

Table 4.525. Arguments for HTTPClient::setNoDelay()

Argument

Description

softbool$nodelay = True

The TCP_NODELAY setting for the HTTPClient.


Table 4.526. Return Value for HTTPClient::setNoDelay()

Return Type

Description

int

0 for success, non-zero for errors. To get error information, see errno() and strerror().


4.10.39. HTTPClient::isConnected()

Synopsis

Returns the connection state of the HTTPClient object. Connections are implicitly established whenever a method is callde requiring a connection; to explicitly establish a connection, see HTTPClient::connect().

Prototype

boolHTTPClient::isConnected()

Example
my bool $connected = $httpclient.isConnected();

Table 4.527. Return Value for HTTPClient::isConnected()

Return Type

Description

bool

The connection state of the HTTPClient object.


There are no comments yet

Leave a Comment



?
? ?
?

 
Powered by TalkBack