AgileApps Support Wiki Pre Release

Difference between revisions of "HttpConnection Class"

From AgileApps Support Wiki
imported>Aeric
 
imported>Aeric
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
===About the {{PAGENAME}}===
The <tt>HttpConnection</tt> class makes a HTTP connection to a given URI. You can use it to make GET and POST requests to other web services.  
The <tt>HttpConnection</tt> 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.  
;Considerations:
:* HTTPS calls are supported only for URIs that have standard security certificates.  


When you call the  <tt>execute</tt> method in <tt>HttpConnection</tt>, 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.
:* When you call the  <tt>execute</tt> method in <tt>HttpConnection</tt>, 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 <tt>HttpConnection</tt> object.
:* Make sure that the URL is properly encoded when instantiating an <tt>HttpConnection</tt> object.  
 
''Learn more:''
:* [[URL Encoding]]
:* [[HowTo:Use the HttpConnection Class to access a web service]]


=== Constructor ===
=== Constructor ===
Line 11: Line 17:
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 encoded_URI)</pre>


:;methodType:Sets the HTTP method type:  
:;methodType:Sets the HTTP method type:  
::* CONSTANTS.HTTP.METHOD.GET for HTTP GET requests
::* CONSTANTS.HTTP.METHOD.GET for an HTTP GET request
::* CONSTANTS.HTTP.METHOD.POST for HTTP POST requests
::* CONSTANTS.HTTP.METHOD.POST for an HTTP POST request
:;String URI:URI to which to connect
:;String encoded_URI:Properly encoded URI to connect to<br>''Learn more:'' [[URL Encoding]]


=== 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 49:
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>


:'''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 63:
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 72:
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 81:
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 95:
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 122:
|
|
<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 129:
|}
|}


<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 20:58, 12 March 2014

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".

<syntaxhighlight lang="java" enclose="div"> 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(); </syntaxhighlight >

Notepad.png

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