Search This Blog

Wednesday, May 13, 2009

How to read a SSO protected page

You can use wget to get the SSO protected page using the following command


wget --debug --http-user=oc4jadmin   --http-password=welcome1  -O dd.txt http://xxx.xxx.oracle.com:7777/private/dd.txt"

You can use the following code to get the SSO protected page. Download the following jar files
commons-logging.jar , httpcore-4.0.jar , commons-codec-1.3.jar , httpclient-4.0-beta2.jar and
httpmime-4.0-beta2.jar

Run the java program with the following options

d:\jdev10gmin\jdk\bin\javaw.exe -client -classpath D:\jdev10gmin\jdev\mywork\Testing\HTTPClient\classes;E:\ias10134\webservices\lib\commons-logging.jar;D:\httpcomponents-core-4.0\lib\httpcore-4.0.jar;D:\commons-codec-1.3\commons-codec-1.3.jar;D:\httpcomponents-client-4.0-beta2\lib\httpclient-4.0-beta2.jar;D:\httpcomponents-client-4.0-beta2\lib\httpmime-4.0-beta2.jar httpclient.ClientAuthentication1

package httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* A simple example that uses HttpClient to execute an HTTP request against
* a target site that requires user authentication.
*/
public class ClientAuthentication1 {

    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(
        new AuthScope("xxx.xxx.oracle.com", 7777),
        new UsernamePasswordCredentials("orcladmin", "welcome1"));
        HttpGet httpget = new HttpGet("http://xxx.xxx.oracle.com:7777/private/dd.txt");
        System.out.println("executing request \n" + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
              System.out.println("None");
            } else {
              for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
              }
            }
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        InputStream in =  entity.getContent();
        InputStreamReader inputstreamreader =
            new InputStreamReader(in);
        BufferedReader bufferedreader =
            new BufferedReader(inputstreamreader); 
        System.out.println("--------- URL Contents -----------------");    
        String line;
        while ((line = bufferedreader.readLine())
                  != null) {
            System.out.println(line);
        }
        bufferedreader.close();
        inputstreamreader.close();
                   in.close();   
        System.out.println("----------------------------------------");
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        if (entity != null) {
            entity.consumeContent();
        }

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();       
    }
}