Difference between revisions of "HttpConnection Class"

From LongJump Support Wiki
imported>Aeric
 
imported>Aeric
 
(18 intermediate revisions by the same user not shown)
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 19: Line 19:


=== Methods ===
=== Methods ===
The methods are:
:*[[#addHeader|<tt>addHeader</tt>]]
:*[[#addParameter|<tt>addParameter</tt>]]
:*[[#execute|<tt>execute</tt>]]
:*[[#getResponse|<tt>getResponse</tt>]]
:*[[#encode|<tt>encode</tt>]]
:*[[#getResponseHeaders|<tt>getResponseHeaders</tt>]]
:*[[#setRequestBody|<tt>setRequestBody</tt>]]


====addHeader====
====addHeader====
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 43:
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 41: Line 49:
:'''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 49: Line 57:
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 66:
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 75:
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


====getResponseHeaders====
====getResponseHeaders====
This method will retrieve the headers from the HTTP Response from an external server. It is a map of String Header Name/Header Value pairs.  
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 <tt>"Content-Length"</tt> is the Header Name and <tt>"10008"</tt> is the Header Value.
For example, if the Header Content-Length is equal to 10008, then <tt>"Content-Length"</tt> is the Header Name and <tt>"10008"</tt> is the Header Value.
Line 81: Line 89:
This method is to be called after execute method of HttpConnection.  
This method is to be called after execute method of HttpConnection.  


:;Signature of the Method: <tt>HashMap<String name, String value> getResponseHeaders()</tt>
:;Method signature: <tt>HashMap<String name, String value> getResponseHeaders()</tt>


:;Parameters:None
:;Parameters:None


:;Return:A HashMap of Header Name/Header Value string pairs
:;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: <tt>HashMap<String name, String value> getResponseHeaders()</tt>
:;Parameters:
::* '''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 [[CharSet|character set]] used in the request. For example: <tt>"utf-8"</tt>.
:;Return:None
:''Learn more:''
::* [[MIME type]]s
::* [[CharSet]]s


===HttpConnection Example===
===HttpConnection Example===
Line 93: Line 116:
|
|
<syntaxhighlight lang="java" enclose="div">
<syntaxhighlight lang="java" enclose="div">
HttpConnection con = new HttpConnection(CONSTANT.HTTP.METHOD.GET,
HttpConnection con = new HttpConnection(CONSTANTS.HTTP.METHOD.GET,
     "http://www.google.com/search?hl=en&q=IT+Leaders&btnG=Google+Search");
     "http://www.google.com/search?hl=en&q=IT+Leaders&btnG=Google+Search");
int code= con.execute();  
int code= con.execute();  
Line 100: Line 123:
|}
|}


<noinclude>[[Category:Support Classes and Objects]]</noinclude>
{{Note|The typical response format is JSON. Those libraries are built into the platform. The APIs are documented at [http://www.json.org/java/index.html JSON.org].}}
<noinclude>
 
[[Category:Support Classes and Objects]]
</noinclude>

Latest revision as of 01:28, 17 April 2012

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

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.