The following examples show how to use the secure connection factory to set up HTTPS connections. The first example shows how to establish the connection using SSL server authentication only. The second example shows the same connection using mutual authentication. The third example shows the same connection using user ID and password for the client authentication.
The following example shows how to set up the connection to the site www.mycompany.com using SSL with server authentication only. The keystore used to verify the server’s certificate is the keystore named keystoreCAs, which is located in an arbitrary location, for example, in a file in the file system.
Code Example for Establishing an HTTPS Connection Using SSL Server Authentication
// keystoreCAs
keystore instance with trusted certificates |
The following example establishes the connection to www.mycompany.com using SSL with mutual authentication. The keystore used to verify the server’s certificate is the keystore named keystoreCAs. The client’s own key pair and certificate are stored in the keystore keystoreMyKeys. Both of these keystores are located in an arbitrary location, for example in a file in the file system.
Code Example for Establishing an HTTPS Connection Using SSL Mutual Authentication
// keystoreCAs
keystore instance with trusted certificates |
The following example establishes the connection to www.mycompany.com using SSL with server authentication. To authenticate the client, it uses user ID and password.
Code Example for Using User ID and Password for Client Authentication
// keystoreCAs
keystore instance with trusted certificates |
The following example creates an SSL socket instance using the method createSocket. It returns a socket instance for port 443.
Code Example for Creating an SSL Socket Instance
// Create (SSL)socket |
The following example shows some of the functions available for managing the connection as follows:
● Using the method setRequestMethod, we set the HTTP request method to POST.
● Using the method connect, we connect to the site using our HTTPS connection.
● Using the method getResponseCode, we can obtain the return code to use for error handling.
● Using the Util class’s method loadAsBytes., we can read the HTML page as bytes.
Code Example for Establishing the HTTPS Connection Using a URL
// set request method if necessary (default: GET) |