Difference between revisions of "HttpConnection Class"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
 
(20 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.


:;Method signature:<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====
Line 37: Line 51:
:'''Method signature'''
:'''Method signature'''


:<pre>void addParameter(String key, String value)</pre>
::<pre>void addParameter(String key, String value)</pre>


:'''Parameters'''
:'''Parameters'''
Line 87: Line 101:
:;Return:A HashMap of Header Name/Header Value string pairs
:;Return:A HashMap of Header Name/Header Value string pairs


====setRequestBody(body)====
====setRequestBody====
Specify the body of the request, using the default content type and character set specified by the HTTP protocol (<tt>text/xml</tt> and <tt>utf-8</tt>).
Specify the body of the request, the content type, and character set.
 
:;Method signature: <tt>HashMap<String name, String value> getResponseHeaders()</tt>
 
:;Parameters:None
 
:;Return:None
 
====setRequestBody(body, ...)====
Specify the body of the request, along with the content type and character set.


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


:;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 [[CharSet|character set]] used in the request. For example: <tt>"utf-8"</tt>.
::* '''charSet -''' String. The [[CharSet|character set]] used in the request. For example: <tt>"utf-8"</tt>.


Line 108: Line 113:


:''Learn more:''
:''Learn more:''
::* [[Mime type]]s
::* [[MIME type]]s
::* [[CharSet]]s
::* [[CharSet]]s


Line 117: 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 123: Line 128:
</syntaxhighlight >
</syntaxhighlight >
|}
|}
{{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>
<noinclude>


[[Category:Support Classes and Objects]]
[[Category:Support Classes and Objects]]
</noinclude>
</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".

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.