Hi , Requirement: We want to simulate following scenario with HttpClient. Log on to website which requires Digital Certificate. After that click on a link of Welcome Page and get the response and based on new page again do some processing on the same session.
In another sense we can say Text based browser. Current Status of Solution: I have written a small piece of code for two-factor authentication using apache HTTPClient and AuthSSLProtocolSocketFactory as per SSL Guide. Authentication is successful by passing client certificate with private key and server's truststore and respective password. Response from the server is chunk of html (Welcome/Home Page) . My requirement is, to follow the hyper-link present in the response html (Welcome/Home Page). I mean i want to click on test-hyper-link and get the response. Is there any way to do this ? Please find the java code used below. base url : https://abcdev.test.com Link URL on Welcome Page: https://abcdev.test.com/ABCProject/servlet/PageDirector?choice=upload&userRole=ABCUser It can be Get Method or Post Method in real scenario. We tried with relative, compelete URL also. Also we passed Name Value pair. Our current understanding is we have to use same method which we used for first request/login. Becuase it is storing the actual connection and session. -Sanjeev JAVA CODE USED: ------------------------- import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.util.Properties; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient ; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpRecoverableException; import org.apache.commons.httpclient.HttpVersion ; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HostParams ; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.log4j.Logger ; import org.apache.log4j.PropertyConfigurator; /** * Client Application is command line version of the Web based. * */ public class ABCCLi { /** * static variable to hold the logger class name. */ private static Logger ourLogger = Logger .getLogger(com.adp.ABC.cli.ABCCLi.class); private static String strClientKeyStore; private static String strClientKeyStorePassword; private static String strServerTrustStore; private static String strServerTrustStorePassword; private static String strABCServerURL; private static String strABCUserName; private static String strABCPassword; public static final int HTTP_FAILURE = -1; public static final int HTTP_SUCCESS = 200; public static final int HTTP_AUTHENTICATION_ERROR = 401; public static final int HTTP_FILE_NOT_FOUND = 404; private static final int RETRY_COUNT = 5; private int statusCode = HTTP_FAILURE; private static Protocol authhttps, easyhttps; private String lastModifiedDate = null; /** * main entry for the application * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //load the log4j config PropertyConfigurator.configure(args[1]); ourLogger.info("java.home:" + System.getProperty("java.home")); ourLogger.info("java.library.path:" + System.getProperty("java.library.path")); ourLogger.info ("java.endorsed.dirs:" + System.getProperty("java.endorsed.dirs")); ourLogger.info("sun.boot.library.path:" + System.getProperty("sun.boot.library.path")); ourLogger.info ("java.ext.dirs:" + System.getProperty("java.ext.dirs")); //load and set the properties setProperties(args[0]); ABCCLi ABCcli = new ABCCLi(); String msg = new String(ABCcli.getResponse(strABCServerURL, strABCUserName, strABCPassword)); //ourLogger.info("Message::>" + msg); } /** * Default Constructor. Registering of the protocol occures here. * */ public ABCCLi() { try { authhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(new URL("file:" + strClientKeyStore), strClientKeyStorePassword, new URL("file:" + strServerTrustStore), strServerTrustStorePassword), 443); Protocol.registerProtocol("https", authhttps); } catch (Exception ex) { ourLogger.error("Error while registering Protocol", ex); ex.printStackTrace(); } } /** * This method returns the response in the form of byte array. * * @param url - * Secure URL of the server to be connected. * @return byte array of the response * @throws Exception */ public byte[] getResponse(String url) throws Exception { return getResponse(url, null, null); } /** * This method returns the response in the form of byte array given username * and password. * * @param url - * Secure URL of the server to be connected. * @param username - * User name for the secure site * @param password - * Password for the secure site * @return byte array of the response * @throws Exception */ public byte[] getResponse(String url, String username, String password) throws Exception { String SMSESSION = null; String ABCJSESSIONID = null; String SMCHALLENGE = null; String SMIDENTITY = null; // Create an instance of HttpClient. HttpClient client = new HttpClient(); HttpVersion ver = (HttpVersion) client.getParams().getParameter( "http.protocol.version"); ourLogger.info("----------------- Http Version : " + ver.getMajor() + "." + ver.getMinor()); client.getHttpConnectionManager().getParams().setBooleanParameter( "http.connection.stalecheck", true); if (username != null) { // pass our credentials to HttpClient, they will only be used for // authenticating to servers with realm "realm", to authenticate // against an arbitrary realm change this to null. client.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials( username, password); client.getState().setCredentials( new AuthScope(AuthScope.ANY_HOST , AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds); } // Execute the method. byte[] responseBody = null; //Perform Recoverable Retry for (int i = 0; statusCode == -1 && i < RETRY_COUNT; ++i) { // Create a method instance. // url = https://abcdev.test.com HttpMethod method = new GetMethod(url); client.getHostConfiguration().setParams(new HostParams()); method.setRequestHeader("Connection", "Keep-Alive"); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(5, false)); method.setDoAuthentication(true); method.setFollowRedirects(true); try { HttpConnectionParams httpConnectionParams = new HttpConnectionParams(); httpConnectionParams.setSoTimeout(10 * 1000); ourLogger.info("executing method client.executeMethod(method)"); // execute the method. statusCode = client.executeMethod(method); ourLogger.debug("statusCode = " + statusCode); responseBody = method.getResponseBody(); Header lastModifiedHdr = method .getResponseHeader("Last-Modified"); if (lastModifiedHdr != null) lastModifiedDate = (String) lastModifiedHdr.getValue(); ourLogger.info("Clicking on link of welcome page...."); // We tried with relative and compelte URL also, // Tested with - add as Name Value Pair for parameters also // It is not working method.setPath ("/ABCProject/servlet/PageDirector?choice=upload&userRole=ABCUser"); method.setRequestHeader("Connection", "Keep-Alive"); statusCode = client.executeMethod (method); ourLogger.debug("statusCode = " + statusCode); responseBody = method.getResponseBody(); return responseBody; } catch (HttpRecoverableException e) { if (2 >= 1) { e.printStackTrace(); throw new Exception("ABCCLi.getResponse.unRecovered "); } else { ourLogger .error("A recoverable exception occurred, retrying." + e.getMessage()); } } catch (HttpException e) { ourLogger.error("Failed to access the url " + url + ": " + e.getMessage()); e.printStackTrace(); throw new Exception("ABCCLi.getResponse.HttpException"); } catch (IOException e) { ourLogger.error("Failed to access the url " + url + ": " + e.getMessage()); e.printStackTrace(); throw new Exception("ABCCLi.getResponse.IOException"); } finally { // Release the connection. method.releaseConnection(); } } return responseBody; } /** * Returns the status code * * @return status code */ public int getStatusCode() { return statusCode; } /** * Returns the last modified date * * @return last modified date */ public String getLastModifiedDate() { return lastModifiedDate; } /** * Method to perform the loading of the properties from the properties file * and sets to the class level variables. * * @param pPropertyFile */ private static void setProperties(String pPropertyFile) { ourLogger.info("Loading Properties file & setting to member variables"); Properties prop; FileInputStream fis; try { fis = new FileInputStream(pPropertyFile); prop = new Properties(); prop.load(fis); //Fetching values from the properties file strClientKeyStore = prop.getProperty("client_keystore"); ourLogger.debug("strClientKeyStore=" + strClientKeyStore); strClientKeyStorePassword = prop .getProperty("client_keystore_password"); ourLogger.debug("strClientKeyStorePassword=" + strClientKeyStorePassword); strServerTrustStore = prop.getProperty("server_certificate"); ourLogger.debug("strServerTrustStore=" + strServerTrustStore); strServerTrustStorePassword = prop .getProperty("server_truststore_password"); ourLogger.debug("strServerTrustStorePassword=" + strServerTrustStorePassword); // URL is https://abcdev.test.com strABCServerURL = prop.getProperty("ABC_server_url"); ourLogger.debug("strABCServerURL=" + strABCServerURL); strABCUserName = prop.getProperty("user_name"); ourLogger.debug("strABCUserName=" + strABCUserName); strABCPassword = prop.getProperty("password"); ourLogger.debug("strABCPassword=" + strABCPassword); } catch (FileNotFoundException fileNotFoundExp) { ourLogger.error("FileNotFoundException() : " + fileNotFoundExp); } catch (IOException ioExp) { ourLogger.error("IOException : " + ioExp); } catch (NullPointerException nullPointerExp) { ourLogger.error("NullPointerException : " + nullPointerExp); } catch (Exception excep) { ourLogger.error("Exception : " + excep); } } }
