Class: Client

$.net.http. Client

HTTP(s) Client for outbound connectivity. This client supports HTTP and HTTPs connections over HTTP or SOCKS proxy. You can either use a destination (preferred way) or a URL as target. To use HTTPs you need to specify a trust store with the needed certificates (either in the destination or with setTrustStore).

To choose between HTTP and SOCKS proxy, the proxy URL starts with either "http://" or "socks://".

This HttpClient is equipped with a cookie database. If a previous response sent a "set-cookie" header, the cookie is stored for the relevant domain and path. Subsequent requests will be enriched with the stored cookies automatically.

new Client()

Example
// create client
var client = new $.net.http.Client();

// where and what to send
var dest = $.net.http.readDestination("testApp", "myDestination");
var request = new $.net.http.Request($.net.http.GET, "/"); // new Request(METHOD, PATH)
                                                           // the PATH will be prefixed by destination's pathPrefix, e.g. "/search?" on the request

// send the request and synchronously get the response
client.request(request, dest);
var response = client.getResponse();


// get all the cookies and headers from the response
var co = [], he = [];
for(var c in response.cookies) {
    co.push(response.cookies[c]);
}

for(var c in response.headers) {
     he.push(response.headers[c]);
}

// get the body
var body;
if(!response.body)
    body = "";
else
    body = response.body.asString();

// close the connection
client.close();        // prevent socket leak - see xsengine.ini: [communication] - max_open_sockets_per_request

// send the response as JSON
$.response.contentType = "application/json";
$.response.setBody(JSON.stringify({"status": response.status, "cookies": co, "headers": he, "body": body}));

Methods

close()

Close the connection and decrement the sockets_per_request count.
See:
  • xsengine.ini: [communication] - max_open_sockets_per_request

getResponse() → {$.web.WebResponse}

Retrieve the response from the previously sent request synchronously/blocking
Throws:
Throws an error if there is no valid response to return
Returns:
A response object with headers, cookies and the response body
Type
$.web.WebResponse

request(request, destination)

Send a new request object to the given destination
Parameters:
Name Type Description
request $.net.http.Request The request object to send
destination $.net.http.Destination The destination object that holds the metadata of the host to send the request to
Throws:
Throws an error if the request fails or the parameters are invalid

request(request, url, proxy)

Send a request object to the given URL
Parameters:
Name Type Argument Description
request $.net.http.Request The request object to send
url String URL to send the request to, e.g. "http://www.google.de/q=HANA"
proxy String <optional>
Proxy URL to use for this request, e.g. "http://proxy.mycompany.com:3128"
Throws:
Throws an error if the request fails or the parameters are invalid

request(WebMethod, url, proxy)

Send a new request to the given URL, using the specified HTTP method
Parameters:
Name Type Argument Description
WebMethod $.net.http The HTTP Method to use for this request ($.net.http.GET/POST/PUT/...)
url String URL to send the request to, e.g. "http://www.google.de/q=HANA"
proxy String <optional>
Proxy URL to use for this request, e.g. "http://proxy.mycompany.com:3128"
Throws:
Throws an error if the request fails or the parameters are invalid

setTimeout(timeout)

Sets the timeout for communication with the server
Parameters:
Name Type Description
timeout Number Timeout in seconds
Throws:
Throws an error if the parameter is not a numeric value

setTrustStore(trustStore)

Sets the default trust store the will be used when issuing https:// requests via request(request, URI, ...)-syntax.
Parameters:
Name Type Description
trustStore String name