The WebClient class is used to access web servers.
Constructor
var client = new WebClient();
Properties
There are no WebClient properties accessible from JavaScript.
Methods
Return Value | Name | Description |
void | addParameter( String name , Object value ) | Adds the name/value pair as a parameter to a GET or POST method. value is optional, but can only be missing for the first parameter of a GET method. Other parameters with no value specified result in name=null . This allows for a URL in a GET method. If you need more than one parameter with no value in a GET method, you must specify the parameters in the URL and encode the parameters manually. |
void | clearParameters() | Clears all parameters. |
int | doGetMethod( String url ) | Creates and executes an HTTP GET method with the given url and any previously specified parameters. Returns the status of the GET method |
int | doPostMethod( String url ) | Creates and executes an HTTP POST method with the given url and any previously specified parameters. Returns the status of the POST method. |
String | getResponseBody() | Returns the response body of the last executed method. If the response body is not available or cannot be read, returns null. |
int | getStatusCode() | Returns the response status code of the last executed method |
String | getStatusText() | Returns the status text (or "reason phrase") associated with the latest response. |
void | releaseConnection() | Releases the connection being used by the last executed HTTP method. In particular the connection is used to read the response (if there is one) and will be held until the response has been read. If the connection can be reused by other HTTP methods, it is NOT closed at this point |
void | setConnectionTimeout( int millis ) | Sets the timeout until a connection is established. A value of zero means the timeout is not used. The default value is zero. |
void | setHost( String host , int port , String protocol ) | Sets the given host, port and protocol needed to describe an HTTP connection to a host. port and protocol are optional. |
void | setProxy( String host , int port ) | Sets the proxy settings. port is optional and defaults to http (80). |
void | setProxyCredentials( String username , String password ) | Sets the user credentials for the proxy host used for both BASIC and DIGEST authentication schemes. setProxy must be called before calling this method since the credentials are given an authentication scope of the host and port of the proxy. |
void | setRetries( int retries ) | Sets the number of times an HTTP method will be retried. This is the default value used for all methods associated with this client. |
void | setUserCredentials( String username , String password ) | Sets the user credentials used for both BASIC and DIGEST authentication schemes. setHost must be called before calling this method since the credentials are given an authentication scopr of the host and port . |
Sample usage
var client = new WebClient(); client.setHostConfiguration( action.server, action.prot, action.protocol ); if ( action.use_auth == ‘true’ ) { client.setUserCredentials( action.username, action.password ); } var resultCode = client.doGetMethod( action.url ); if ( resultCode == 200 ) { logger.info( “URL succeeded: “ + action.url ); } else if ( resultCode == -1 ) { logger.error( “URL failed: “ + resultCode + “: Check logfile for errors” ); } else { logger.error( “URL failed: “ + resultCode + “: “ + client.getStatusText() ); } var body = client.getResponseBody();