AgileApps Support Wiki Pre Release

Difference between revisions of "HttpConnection Class"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 11: Line 11:
You create an instance of <tt>HttpConnection</tt> using the <tt>new</tt> operator.
You create an instance of <tt>HttpConnection</tt> using the <tt>new</tt> operator.


:;Signature of the Method:<pre>HttpConnection con = new HttpConnection(int methodType, String URI)</pre>
:;Method signature:<pre>HttpConnection con = new HttpConnection(int methodType, String URI)</pre>


:;methodType:Sets the HTTP method type:  
:;methodType:Sets the HTTP method type:  
Line 23: Line 23:
Adds a key-value pair to the request header.
Adds a key-value pair to the request header.


:;Signature of the Method:<pre>void addHeader(String key, String value)</pre>
:;Method signature:<pre>void addHeader(String key, String value)</pre>


:'''Parameters'''
:'''Parameters'''


:;key:The name of the key
::* '''key -''' The name of the key
:;value:The value of the key
::* '''value -''' The value of the key


:;Return:None
:;Return:None
Line 35: Line 35:
Adds a parameter to an HTTP POST request. This methods throws an exception if it is called for an HTTP GET request.
Adds a parameter to an HTTP POST request. This methods throws an exception if it is called for an HTTP GET request.


:'''Signature of the Method'''
:'''Method signature'''


:<pre>void addParameter(String key, String value)</pre>
:<pre>void addParameter(String key, String value)</pre>
Line 49: Line 49:
Executes the request and returns a standard HTTP response such as "200 - OK" or "404 - not found".
Executes the request and returns a standard HTTP response such as "200 - OK" or "404 - not found".


:;Signature of the Method:<pre>int execute()</pre>
:;Method signature:<pre>int execute()</pre>


:;Parameters:None
:;Parameters:None
Line 58: Line 58:
Returns the response as a <tt>String</tt>.
Returns the response as a <tt>String</tt>.


:;Signature of the Method:<pre>String getResponse()</pre>
:;Method signature:<pre>String getResponse()</pre>


:;Parameters:None
:;Parameters:None
Line 67: Line 67:
Returns a String encoded from <tt>s</tt>.
Returns a String encoded from <tt>s</tt>.


:;Signature of the Method:<pre>String encode(String s)</pre>
:;Method signature:<pre>String encode(String s)</pre>


:;Parameters:
:;Parameters:
::;s:The string to encode
::* '''s -''' The string to encode


:;Return:The encoded string
:;Return:The encoded string
Line 102: Line 102:


:;Parameters:
:;Parameters:
:* '''content-Type -''' String. One of the valid [[Mime type]]s for the body of a request. For example: <tt>"application/xml"</tt>.
::* '''content-Type -''' String. One of the valid [[Mime type]]s for the body of a request. For example: <tt>"application/xml"</tt>.
:* '''charSet -''' String. The character set used in the request. For example: <tt>"utf-8"</tt>.
::* '''charSet -''' String. The character set used in the request. For example: <tt>"utf-8"</tt>.


:;Return:None
:;Return:None
Line 119: Line 119:
</syntaxhighlight >
</syntaxhighlight >
|}
|}
<noinclude>


<noinclude>[[Category:Support Classes and Objects]]</noinclude>
[[Category:Support Classes and Objects]]
</noinclude>

Revision as of 23:43, 8 August 2011

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.

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.

Note: Make sure that the URL is properly encoded when instantiating an HttpConnection object.

Constructor

You create an instance of HttpConnection using the new operator.

Method signature
HttpConnection con = new HttpConnection(int methodType, String URI)
methodType
Sets the HTTP method type:
  • CONSTANTS.HTTP.METHOD.GET for HTTP GET requests
  • CONSTANTS.HTTP.METHOD.POST for HTTP POST requests
String URI
URI to which to connect

Methods

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(body)

Specify the body of the request, using the default content type (__TBD__) and character set (__TBD__).

Method signature
HashMap<String name, String value> getResponseHeaders()
Parameters
None
Return
None

setRequestBody(body, ...)

Specify the body of the request, along with 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

HttpConnection Example

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

<syntaxhighlight lang="java" enclose="div"> HttpConnection con = new HttpConnection(CONSTANT.HTTP.METHOD.GET,

   "http://www.google.com/search?hl=en&q=IT+Leaders&btnG=Google+Search");

int code= con.execute(); String response = con.getResponse(); </syntaxhighlight >