Difference between revisions of "HttpConnection Class"
imported>Aeric |
imported>Aeric |
||
(12 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. | ||
:* 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 | :;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 | ::* CONSTANTS.HTTP.METHOD.GET for an HTTP GET request | ||
::* CONSTANTS.HTTP.METHOD.POST for HTTP POST | ::* CONSTANTS.HTTP.METHOD.POST for an HTTP POST request | ||
:;String | :;String encoded_URI:Properly encoded URI to connect to<br>''Learn more:'' [[URL Encoding]] | ||
=== Methods === | === Methods === | ||
Line 45: | 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 101: | Line 107: | ||
:;Parameters: | :;Parameters: | ||
::* '''content-Type -''' String. One of the valid [[ | ::* '''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 107: | Line 113: | ||
:''Learn more:'' | :''Learn more:'' | ||
::* [[ | ::* [[MIME type]]s | ||
::* [[CharSet]]s | ::* [[CharSet]]s | ||
Line 122: | 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".
<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 >
Note: The typical response format is JSON. Those libraries are built into the platform. The APIs are documented at JSON.org.