public class HttpClient
extends java.lang.Object
connectionUrl property
and then simply add some methods that would be used to contact some different sub-urls with the same url base. Those
methods may receive some parameters that would enable to build final url based on pre-set base url. However this
class may be used on its own as well. All you will need to do is to set a value for content type using method
setContentType(String) if needed, or/and any other request property using method
setRequestProperty(String, String) and then just call method sendHttpRequest(String, HttpMethod)
or sendHttpRequest(String, HttpMethod, String) (or if the reply is expected to be binary such as image then
call the methods sendHttpRequestForBinaryResponse(String, HttpMethod) or
sendHttpRequestForBinaryResponse(String, HttpMethod, String))| Modifier and Type | Class and Description |
|---|---|
static class |
HttpClient.HttpMethod |
| Constructor and Description |
|---|
HttpClient() |
| Modifier and Type | Method and Description |
|---|---|
void |
clearAllRequestProperties()
This method clears all request properties.
|
java.lang.String |
getConnectionUrl()
This method is a getter for connectionUrl property which only makes sense if that property was set.
|
java.lang.String |
getContentType()
This is getter method for content type property.
|
protected java.lang.String |
getDefaultContentType()
This method is intended to be overridden by extending classes if those classes provide methods that send
requests with the same content type
|
protected java.net.HttpURLConnection |
openHttpUrlConnection(java.lang.String url,
HttpClient.HttpMethod method)
This method creates and opens
HttpURLConnection to specified URL The connection's property
doOutput is set to false |
protected java.net.HttpURLConnection |
openHttpUrlConnection(java.lang.String url,
HttpClient.HttpMethod method,
boolean doOutput)
This method creates and opens
HttpURLConnection to specified URL. |
java.lang.String |
removeRequestProperty(java.lang.String propertyName)
This method removes request property
|
java.lang.String |
sendHttpRequest(HttpClient.HttpMethod callMethod)
This method sends HTTP request to pre-set URL.
|
java.lang.String |
sendHttpRequest(HttpClient.HttpMethod callMethod,
java.lang.String data)
This method sends HTTP request to pre-set URL.
|
java.lang.String |
sendHttpRequest(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod)
This method sends HTTP request to specified URL and uses specified HTTP method.
|
java.lang.String |
sendHttpRequest(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod,
java.lang.String data)
This method sends HTTP request to specified URL, uses specified HTTP method and sends data through request body.
|
java.nio.ByteBuffer |
sendHttpRequestForBinaryResponse(HttpClient.HttpMethod callMethod)
This method sends HTTP request to pre-set URL.
|
java.nio.ByteBuffer |
sendHttpRequestForBinaryResponse(HttpClient.HttpMethod callMethod,
java.lang.String data)
This method sends HTTP request to pre-set URL.
|
java.nio.ByteBuffer |
sendHttpRequestForBinaryResponse(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod)
This method is the same as
sendHttpRequest(String, HttpMethod) except that it returns ByteBuffer
that holds binary info. |
java.nio.ByteBuffer |
sendHttpRequestForBinaryResponse(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod,
java.lang.String data)
This method is the same as
sendHttpRequest(String, HttpMethod, String) except that it returns
ByteBuffer that holds binary info. |
void |
setConnectionUrl(java.lang.String connectionUrl)
Setter for connectionUrl property.
|
void |
setContentType(java.lang.String contentType)
This is setter method for content type property.
|
void |
setRequestProperty(java.lang.String propertyName,
java.lang.String value)
This method is a general request property setter.
|
public java.lang.String getConnectionUrl()
public void setConnectionUrl(java.lang.String connectionUrl)
connectionUrl - public java.lang.String sendHttpRequest(HttpClient.HttpMethod callMethod) throws java.io.IOException
getConnectionUrl() to get the URL and uses
specified HTTP method. Obviously it is expected that user should set connectionUrl property by invoking method
setConnectionUrl(String) beforehand. It returns response
that is expected to be textual such as a String. This method does not send any info through request body.
It may send parameters through URL. So this method fits perfectly for using HTTP GET method. If HTTP request
uses method POST, PUT or any other methods that allow passing info in the request body and there is some info
to be sent then use method sendHttpRequest(HttpMethod, String)callMethod - HttpClient.HttpMethod that specifies which HTTP method is to be usedjava.io.IOExceptionpublic java.lang.String sendHttpRequest(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod)
throws java.io.IOException
sendHttpRequest(String, HttpMethod, String)requestUrl - URL to which request is to be sentcallMethod - HttpClient.HttpMethod that specifies which HTTP method is to be usedjava.io.IOExceptionpublic java.lang.String sendHttpRequest(HttpClient.HttpMethod callMethod, java.lang.String data) throws java.io.IOException
getConnectionUrl() to get the URL and uses
specified HTTP method and sends data through request body. Obviously it is expected that user should set connectionUrl
property by invoking method setConnectionUrl(String) beforehand.
It returns response that is expected to be textual such as a String. This method fits for using HTTP methods
POST, PUT or any other methods that allow passing info in the body request and there is some info to be sent.
If you don't need to send any info as request body, consider using method
sendHttpRequest(HttpMethod)callMethod - HttpClient.HttpMethod that specifies which HTTP method is to be useddata - String that holds the data to be sent as request bodyjava.io.IOExceptionpublic java.lang.String sendHttpRequest(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod,
java.lang.String data)
throws java.io.IOException
sendHttpRequest(String, HttpMethod)requestUrl - URL to which request is to be sentcallMethod - HttpClient.HttpMethod that specifies which HTTP method is to be useddata - String that holds the data to be sent as request bodyjava.io.IOExceptionpublic java.nio.ByteBuffer sendHttpRequestForBinaryResponse(HttpClient.HttpMethod callMethod) throws java.io.IOException
getConnectionUrl() to get the URL and uses
specified HTTP method. Obviously it is expected that user should set connectionUrl property by invoking method
setConnectionUrl(String) beforehand.
This method is the same as sendHttpRequest(HttpMethod) except that it returns ByteBuffer
that holds binary info. So this methods fits for retrieving binary response such as Image, Video, Audio or any
other info in binary format.callMethod - HttpClient.HttpMethod that specifies which HTTP method is to be usedByteBuffer that holds response from URLjava.io.IOExceptionpublic java.nio.ByteBuffer sendHttpRequestForBinaryResponse(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod)
throws java.io.IOException
sendHttpRequest(String, HttpMethod) except that it returns ByteBuffer
that holds binary info. So this methods fits for retrieving binary response such as Image, Video, Audio or any
other info in binary format.requestUrl - URL to which request is to be sentcallMethod - HttpClient.HttpMethod that specifies which HTTP method is to be usedByteBuffer that holds response from URLjava.io.IOExceptionpublic java.nio.ByteBuffer sendHttpRequestForBinaryResponse(HttpClient.HttpMethod callMethod, java.lang.String data) throws java.io.IOException
getConnectionUrl() to get the URL and uses
specified HTTP method. Obviously it is expected that user should set connectionUrl property by invoking method
setConnectionUrl(String) beforehand.
This method is the same as sendHttpRequest(HttpMethod, String) except that it returns
ByteBuffer that holds binary info. So this methods fits for retrieving binary response such as Image,
Video, Audio or any other info in binary format.callMethod - HttpClient.HttpMethod that specifies which HTTP method is to be useddata - String that holds the data to be sent as request bodyByteBuffer that holds response from URLjava.io.IOExceptionpublic java.nio.ByteBuffer sendHttpRequestForBinaryResponse(java.lang.String requestUrl,
HttpClient.HttpMethod callMethod,
java.lang.String data)
throws java.io.IOException
sendHttpRequest(String, HttpMethod, String) except that it returns
ByteBuffer that holds binary info. So this methods fits for retrieving binary response such as Image,
Video, Audio or any other info in binary format.requestUrl - URL to which request is to be sentcallMethod - HttpClient.HttpMethod that specifies which HTTP method is to be useddata - String that holds the data to be sent as request bodyByteBuffer that holds response from URLjava.io.IOExceptionprotected java.lang.String getDefaultContentType()
getContentType())public java.lang.String getContentType()
public void setRequestProperty(java.lang.String propertyName,
java.lang.String value)
propertyName - if the value of property name is blank or null this method does nothingvalue - Holds the value for the propertypublic java.lang.String removeRequestProperty(java.lang.String propertyName)
propertyName - if the value of propertyName is blank or null this method does nothing and returns nullpublic void clearAllRequestProperties()
public void setContentType(java.lang.String contentType)
contentType - holds content type valueprotected java.net.HttpURLConnection openHttpUrlConnection(java.lang.String url,
HttpClient.HttpMethod method)
throws java.io.IOException
HttpURLConnection to specified URL The connection's property
doOutput is set to falseurl - Url to be connected tomethod - HttpClient.HttpMethod that specifies which HTTP Method to useHttpURLConnectionjava.io.IOExceptionprotected java.net.HttpURLConnection openHttpUrlConnection(java.lang.String url,
HttpClient.HttpMethod method,
boolean doOutput)
throws java.io.IOException
HttpURLConnection to specified URL.url - Url to be connected tomethod - HttpClient.HttpMethod that specifies which HTTP Method to usedoOutput - boolean value that specifies how to set connection's doOutput propertyHttpURLConnectionjava.io.IOException