Class HTTPSWrapper

java.lang.Object
org.webpki.net.HTTPSWrapper

public class HTTPSWrapper extends Object
The HTTPSWrapper class makes it possible to establish HTTP/HTTPS connections to web servers and compose HTTP requests.

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

      public void setTrustStore(KeyStore trust_store)
      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

      public static void setDefaultTrustStore(KeyStore trust_store)
      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

      public void setKeyStore(KeyStore key_store, String key_store_password)
      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 file
      key_store_password - Password to private key
      Throws:
      IOException - If something unexpected happens...
      GeneralSecurityException
    • setKeyAlias

      public void setKeyAlias(String keyAlias)
      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

      public void makePostRequest(String url, byte[] data) throws IOException
      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

      public void makePostRequestUTF8(String url, String data) throws IOException
      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

      public void makeGetRequest(String url) throws IOException
      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

      public String getDataUTF8() throws IOException
      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

      public void setRequestMethod(String requestMethod)
      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

      public void setProxy(String host, int port) throws IOException
      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

      public static void setDefaultProxy(String host, int port) throws IOException
      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

      public void setHeader(String name, String value) throws IOException
      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

      public String getResponseMessage()
      Returns the HTTP response line for the most recent request.
      Returns:
      The response message.
    • getHeaderValue

      public String getHeaderValue(String name)
      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

      public String[] getHeaderValues(String name)
      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

      public LinkedHashMap<String,ArrayList<String>> getHeaders()
      Returns the response headers for this call.
      Returns:
      Structure containing all the headers and associated values for this call.
    • getRawContentType

      public String getRawContentType()
      Gets content type of HTTP response.
      Returns:
      The full Content-Type of the HTTP response, or null if not available.
    • getContentType

      public String 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

      public String 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

      public X509Certificate[] getServerCertificates()
      Gets server certificates from HTTPS response.
      Returns:
      Server certificate path.
    • setUserIDPassword

      public void setUserIDPassword(String user_id, String password)
    • main

      public static void main(String[] argv) throws Exception
      Command-line interface to the HTTPSWrapper.
      Parameters:
      argv - Command line parameters
      Throws:
      Exception - If something unexpected happens...