Class HTTPSWrapper
When making HTTP requests, the wrapper class does the following:
- Sets up http connections to a web server.
- Performs a GET or POST request to the server.
- Receives the response from the Web Server.
When making HTTPS requests, the wrapper class does the following:
- Initializes a trust store for verifying server certificates.
- Sets up https connections to an ssl enabled web server.
- Performs a GET or POST request to the server.
- Configures the SSL client to allow:
- Untrusted server certificates.
- Server certificates with invalid validity period.
- Server certificates which Common Name does not match the
server DNS name.
- Receives the response from the Web Server.
In addition to the above mentioned features, the wrapper class makes it
possible to set and get HTTP request_headers and use proxy (firewall) servers.
When posting data to a server, the default content-type is
application/x-www-form-urlencoded
Code example how to use the HTTPSWrapper.
try { HTTPSWrapper wrap = new HTTPSWrapper (); // Allow untrusted server certificates. wrap.allowInvalidCert (true); // Set HTTP request header. Must be set before making the request. wrap.setHeader ("MyStuff", "IsGreat"); // If not called, the wrapper will use the Trust Store // supplied with the JRE. Must be called before making the request. wrap.setTrustStore ("Path to my store", "passphrase"); // Make the request. wrap.makeGetRequest ("https://www.example.com"); // Read the server response as a byte array. byte[] data = wrap.getData (); }
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
allowDiffHostNames
(boolean flag) If true, client allows Common Name in server certificate to differ from the server DNS name.void
allowInvalidCert
(boolean flag) If true, client allows server certificates that are either not valid yet or have expired.Gets content encoding of HTTP response.int
Gets content length from HTTP response.Gets content type of HTTP response.byte[]
getData()
Gets server response data as byte array.Gets server response data as String in UTF-8 character encoding.Returns the response headers for this call.getHeaderValue
(String name) Returns the value for the corresponding HTTP response header name.String[]
getHeaderValues
(String name) Returns the values for the corresponding HTTP response header name.Gets content type of HTTP response.int
Returns the HTTP response code for the most recent request.Returns the HTTP response line for the most recent request.Gets server certificates from HTTPS response.static void
Command-line interface to the HTTPSWrapper.void
makeGetRequest
(String url) Performs a GET request to the Web Server.void
makePostRequest
(String url, byte[] data) Performs a POST request to the Web Server, posting data without any interpretation of character encoding.void
makePostRequestUTF8
(String url, String data) Performs a POST request to the Web Server, posting data with UTF-8 character encoding.static void
setDefaultProxy
(String host, int port) Sets up the default (app-wide) proxy host and port to use.static void
setDefaultTrustStore
(String store_name, String store_passphrase) Sets the default (app-wide) Trust Store to use.static void
setDefaultTrustStore
(KeyStore trust_store) Sets the default (app-wide) Trust Store to use.void
setFollowRedirects
(boolean flag) Sets wheter or not the HTTPSWrapper should follow HTTP 3xx redirects or not.void
Sets an HTTP header value for a succeeding request.void
setInteractiveMode
(boolean flag) void
setKeyAlias
(String keyAlias) Sets the Key Alias to use.void
setKeyStore
(String key_store_file, String key_store_password) Sets the Key Store to use.void
setKeyStore
(KeyStore key_store, String key_store_password) Sets the Key Store to use.void
Sets up the proxy host and port to use.void
setRequestMethod
(String requestMethod) Override te default methods.void
setRequireSuccess
(boolean flag) If true, responses other HTTP than 200 (OK) will throw an exception.void
setServerCertificateRevocation
(boolean flag) If true, enable server certificate revocation checks.void
setTimeout
(int timeout) Sets the read timeout.void
setTrustStore
(String store_name, String store_passphrase) Sets the Trust Store to use.void
setTrustStore
(KeyStore trust_store) Sets the Trust Store to use.void
setUserIDPassword
(String user_id, String password)
-
Constructor Details
-
HTTPSWrapper
public HTTPSWrapper()Constructs an HTTPSWrapper object.
-
-
Method Details
-
setTrustStore
public void setTrustStore(String store_name, String store_passphrase) throws IOException, GeneralSecurityException Sets the Trust Store to use. The Trust Store is used to verify server certificates in SSL negotiations.
If this method is not called the HTTPSWrapper will use the default JDK Trust Store.
To set your own Trust Store this method must be called before making either a GET or POST request.
- Parameters:
store_name
- Path to the Trust store file.store_passphrase
- Passphrase to unlock Trust Store.- Throws:
IOException
- If something unexpected happens...GeneralSecurityException
-
setDefaultTrustStore
public static void setDefaultTrustStore(String store_name, String store_passphrase) throws IOException, GeneralSecurityException Sets the default (app-wide) Trust Store to use. The Trust Store is used to verify server certificates in SSL negotiations.
If this method is not called the HTTPSWrapper will use the default JDK Trust Store.
- Parameters:
store_name
- Path to the Trust store file.store_passphrase
- Passphrase to unlock Trust Store.- Throws:
IOException
- If something unexpected happens...GeneralSecurityException
-
setTrustStore
Sets the Trust Store to use. The Trust Store is used to verify server certificates in SSL negotiations.
If this method is not called the HTTPSWrapper will use the default JDK Trust Store.
To set your own Trust Store this method must be called before making either a GET or POST request.
- Parameters:
trust_store
- Initialized java KeyStore.
-
setDefaultTrustStore
Sets the default (app-wide) Trust Store to use. The Trust Store is used to verify server certificates in SSL negotiations.
If this method is not called the HTTPSWrapper will use the default JDK Trust Store.
To set your own Trust Store this method must be called before making either a GET or POST request.
- Parameters:
trust_store
- Initialized java KeyStore.
-
setKeyStore
Sets the Key Store to use. The Key Store is used to authenticate with using SSL.
If this method is not called the HTTPSWrapper will use the default JDK Key Store (none).
To set your own Key Store this method must be called before making either a GET or POST request.
- Parameters:
key_store
- Initialized java KeyStore.key_store_password
- Password to private key
-
setKeyStore
public void setKeyStore(String key_store_file, String key_store_password) throws IOException, GeneralSecurityException Sets the Key Store to use. The Key Store is used to authenticate with using SSL.
If this method is not called the HTTPSWrapper will use the default JDK Key Store (none).
To set your own Key Store this method must be called before making either a GET or POST request.
- Parameters:
key_store_file
- PKCS #12 or Java Keystore filekey_store_password
- Password to private key- Throws:
IOException
- If something unexpected happens...GeneralSecurityException
-
setKeyAlias
Sets the Key Alias to use. The Key Alias is used to authenticate with using SSL.
If this method is not called the HTTPSWrapper will look for the first suitable key.
his method must be called before making either a GET or POST request.
- Parameters:
keyAlias
- name of selected key
-
makePostRequest
Performs a POST request to the Web Server, posting data without any interpretation of character encoding.- Parameters:
url
- Fully qualified URL to make connection to.data
- Byte array to post to server.- Throws:
IOException
- If something unexpected happens...
-
makePostRequestUTF8
Performs a POST request to the Web Server, posting data with UTF-8 character encoding.- Parameters:
url
- Fully qualified URL to make connection to.data
- String to post to server.- Throws:
IOException
- If something unexpected happens...
-
makeGetRequest
Performs a GET request to the Web Server.- Parameters:
url
- Fully qualified URL to make connection to.- Throws:
IOException
- If something unexpected happens...
-
getData
public byte[] getData()Gets server response data as byte array.- Returns:
- Byte array containing all received data as it was received from the server, or null if no data was available.
-
getDataUTF8
Gets server response data as String in UTF-8 character encoding.- Returns:
- String containing all received data in UTF-8 encoding, or null if no data was available.
- Throws:
IOException
- If something unexpected happens...
-
setRequestMethod
Override te default methods.
This method affects all proceeding requests.
- Parameters:
requestMethod
- method to use.
-
setRequireSuccess
public void setRequireSuccess(boolean flag) If true, responses other HTTP than 200 (OK) will throw an exception.
This method affects all proceeding requests.
- Parameters:
flag
- True to require success, else false.
-
allowInvalidCert
public void allowInvalidCert(boolean flag) If true, client allows server certificates that are either not valid yet or have expired. Default value is false.
This method affects all proceeding requests.
- Parameters:
flag
- True to allow bad validity certificates, else false.
-
allowDiffHostNames
public void allowDiffHostNames(boolean flag) If true, client allows Common Name in server certificate to differ from the server DNS name. Default value is false.
This method affects all proceeding requests.
- Parameters:
flag
- True to allow different host names, else false.
-
setServerCertificateRevocation
public void setServerCertificateRevocation(boolean flag) If true, enable server certificate revocation checks.
This method affects all proceeding requests.
- Parameters:
flag
- The flag, Yeah
-
setProxy
Sets up the proxy host and port to use.
This method affects all proceeding requests.
- Parameters:
host
- Host name or IP address of proxy.null
removes any previous setting.port
- Port number for proxy connection.- Throws:
IOException
- If something unexpected happens...
-
setDefaultProxy
Sets up the default (app-wide) proxy host and port to use.
This method affects all proceeding requests.
- Parameters:
host
- Host name or IP address of proxy.null
removes any previous setting.port
- Port number for proxy connection.- Throws:
IOException
- If something unexpected happens...
-
setFollowRedirects
public void setFollowRedirects(boolean flag) Sets wheter or not the HTTPSWrapper should follow HTTP 3xx redirects or not. Default is false.This method affects all proceeding requests.
- Parameters:
flag
- Whether or not to follow redirects automatically.
-
setInteractiveMode
public void setInteractiveMode(boolean flag) -
setTimeout
public void setTimeout(int timeout) Sets the read timeout. Default is forever.This method affects all proceeding requests.
- Parameters:
timeout
- Timeout in milliseconds.
-
setHeader
Sets an HTTP header value for a succeeding request.- Parameters:
name
- Name of header to set.value
- Value associated with the header.- Throws:
IOException
- If something unexpected happens...
-
getResponseCode
public int getResponseCode()Returns the HTTP response code for the most recent request.- Returns:
- The response code.
-
getResponseMessage
Returns the HTTP response line for the most recent request.- Returns:
- The response message.
-
getHeaderValue
Returns the value for the corresponding HTTP response header name.- Parameters:
name
- Name to search for in HTTP header.- Returns:
- Value for the requested name, or null if not found. If multiple values exist, only the first found is returned.
-
getHeaderValues
Returns the values for the corresponding HTTP response header name.- Parameters:
name
- Name to search for in HTTP header.- Returns:
- Values for the requested name, or null if not found.
-
getHeaders
Returns the response headers for this call.- Returns:
- Structure containing all the headers and associated values for this call.
-
getRawContentType
Gets content type of HTTP response.- Returns:
- The full
Content-Type
of the HTTP response, or null if not available.
-
getContentType
Gets content type of HTTP response.- Returns:
- The
Content-Type
minus any extra information of the HTTP response, or null if not available.
-
getCharacterEncoding
Gets content encoding of HTTP response.- Returns:
- The character encoding type of the HTTP response, or null if not available.
-
getContentLength
public int getContentLength()Gets content length from HTTP response.- Returns:
- The length of the HTTP response, or -1 if length is not available.
-
getServerCertificates
Gets server certificates from HTTPS response.- Returns:
- Server certificate path.
-
setUserIDPassword
-
main
Command-line interface to the HTTPSWrapper.- Parameters:
argv
- Command line parameters- Throws:
Exception
- If something unexpected happens...
-