AgileApps Support Wiki Pre Release

HttpConnection Class

From AgileApps Support Wiki
Revision as of 20:58, 12 March 2014 by imported>Aeric (→‎Constructor)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

About the HttpConnection Class

The HttpConnection class makes a HTTP connection to a given URI. You can use it to make GET and POST requests to other web services.

Considerations
  • HTTPS calls are supported only for URIs that have standard security certificates.
  • When you call the execute method in HttpConnection, it executes the request, gets the response, and closes the connection. This means that a single object cannot be used multiple times. For multiple HTTP requests, instantiate multiple instances.
  • Make sure that the URL is properly encoded when instantiating an HttpConnection object.

Learn more:

Constructor

You create an instance of HttpConnection using the new operator.

Method signature
HttpConnection con = new HttpConnection(int methodType, String encoded_URI)
methodType
Sets the HTTP method type:
  • CONSTANTS.HTTP.METHOD.GET for an HTTP GET request
  • CONSTANTS.HTTP.METHOD.POST for an HTTP POST request
String encoded_URI
Properly encoded URI to connect to
Learn more: URL Encoding

Methods

The methods are:

addHeader

Adds a key-value pair to the request header.

Method signature
void addHeader(String key, String value)
Parameters
  • key - The name of the key
  • value - The value of the key
Return
None

addParameter

Adds a parameter to an HTTP POST request. This methods throws an exception if it is called for an HTTP GET request.

Method signature
void addParameter(String key, String value)
Parameters
  • key - The name of the key
  • value - The value of the key
Return
None

execute

Executes the request and returns a standard HTTP response such as "200 - OK" or "404 - not found".

Method signature
int execute()
Parameters
None
Return
Standard HTTP response

getResponse

Returns the response as a String.

Method signature
String getResponse()
Parameters
None
Return
The response from the service

encode

Returns a String encoded from s.

Method signature
String encode(String s)
Parameters
  • s - The string to encode
Return
The encoded string

getResponseHeaders

Retrieve the headers from the HTTP Response from an external server. It is a map of String Header Name/Header Value pairs.

For example, if the Header Content-Length is equal to 10008, then "Content-Length" is the Header Name and "10008" is the Header Value.

This method is to be called after execute method of HttpConnection.

Method signature
HashMap<String name, String value> getResponseHeaders()
Parameters
None
Return
A HashMap of Header Name/Header Value string pairs

setRequestBody

Specify the body of the request, the content type, and character set.

Method signature
HashMap<String name, String value> getResponseHeaders()
Parameters
  • content-Type - String. One of the valid MIME types for the body of a request. For example: "application/xml".
  • charSet - String. The character set used in the request. For example: "utf-8".
Return
None
Learn more:

HttpConnection Example

This example gets the result of a Google search for "IT Leaders".

HttpConnection con = new HttpConnection(CONSTANTS.HTTP.METHOD.GET,
    "http://www.google.com/search?hl=en&q=IT+Leaders&btnG=Google+Search");
int code= con.execute(); 
String response = con.getResponse();

Notepad.png

Note: The typical response format is JSON. Those libraries are built into the platform. The APIs are documented at JSON.org.