Hi,

I am trying to login to https://alta.registries.gov.ab.ca/SpinII/Logon.aspx. But I couldn't be successfull. This site use https protocol for login page and after login redirect to http protocol page. Can any body help me? Following is the code. I don't know, where am I making mistake. It is using basic authentication.

import org.htmlparser.*;
import org.htmlparser.filters.TagNameFilter;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.kookie;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.httpclient.methods.GetMethod;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.auth.AuthState;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.htmlparser.util.*;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.tags.InputTag;
import org.htmlparser.filters.HasAttributeFilter;
import org.apache.commons.httpclient.*;
import java.io.*;

public class Test {
static final String hostURI = "alta.registries.gov.ab.ca";
static final int LOGON_PORT = 443;

public static void main(String[] args) {
int status;

String userName = new String("abcdse");
String userPassword = new String("abcdse");
String viewStateValue="";
String javaScripValue = "";
String LOGON_SITE = "https://alta.registries.gov.ab.ca/SpinII/Logon.aspx";


System.getProperties().put("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

// settings for logging information
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");

HttpClient client = new HttpClient();

// This is to make HttpClient pick the Digest authentication for asp.net
List authPrefs = new ArrayList(3);

authPrefs.add(AuthPolicy.DIGEST);

authPrefs.add(AuthPolicy.BASIC);

authPrefs.add(AuthPolicy.NTLM);

client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); //

client.getParams().setParameter("http.socket.timeout", new Integer(10000));

client.getParams().setParameter("http.protocol.content-charset", "UTF-8");

client.getParams().setBooleanParameter("http.protocol.single-cookie-header" , true);

// very weird but site accepts this but not MSIE
client.getParams().setParameter("http.useragent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)");

// do not set preemptive, this forces Basic authentication which is not what we want

client.getParams().setAuthenticationPreemptive(true);//true

client.getParams().setBooleanParameter( HttpClientParams.REJECT_RELATIVE_REDIRECT, false);

client.getParams().setBooleanParameter( HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); //true

client.getHostConfiguration().getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);

client.getHostConfiguration().setHost(hostURI, LOGON_PORT, "https");
client.getHostConfiguration().setHost(hostURI, 80, "http");

HttpState initialState = new HttpState();
initialState.setCredentials(new AuthScope( hostURI, LOGON_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(userName, userPassword));
initialState.setCredentials(new AuthScope( hostURI, 80, AuthScope.ANY_REALM), new UsernamePasswordCredentials(userName, userPassword));
Kookie mycookie = new Kookie(hostURI, "mycookie", "stuff", "/", null, false);
initialState.addCookie(mycookie);

client.setState(initialState);

client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
GetMethod getMethodInfoPage = new GetMethod(LOGON_SITE);

getMethodInfoPage.addRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

getMethodInfoPage.addRequestHeader("Accept-Encoding", "gzip, deflate");

getMethodInfoPage.addRequestHeader("Accept-Language", "en-us");

System.out.println("\n" + "About to make call for getMethodInfoPage connection attempt");
try {
status = client.executeMethod(getMethodInfoPage);

Parser parser = null;
try {

parser = new Parser(new Lexer(getMethodInfoPage.getResponseBodyAsString()));
InputTag node = (InputTag)parser.parse(new HasAttributeFilter("name", "__VIEWSTATE")).elementAt(0);
viewStateValue += node.getAttribute("value");

javaScripValue="1";

System.out.println("End of getting Initial Page"); // there may not be a HEAD tag




}
catch (IOException ex2) {
}
catch (ParserException ex2) {
}



getMethodInfoPage.releaseConnection();
}
catch (IOException ex) {
}

// ******************** now preparing to do POST **********************

NameValuePair[] logindata = new NameValuePair[4];

logindata[0] = new NameValuePair("uctrlLogon_txtLogonName", userName);
logindata[1] = new NameValuePair("uctrlLogon_txtPassword", userPassword);
logindata[2] = new NameValuePair("__VIEWSTATE", viewStateValue);
logindata[3] = new NameValuePair("JavascriptEnabled", javaScripValue);

PostMethod postMethodLoginPage = new PostMethod("https://alta.registries.gov.ab.ca/SpinII/Logon.aspx");

postMethodLoginPage.setDoAuthentication(true);

postMethodLoginPage.addRequestHeader("Connection","Keep-Alive");

postMethodLoginPage.addRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

postMethodLoginPage.addRequestHeader("Accept-Encoding", "gzip, deflate");

postMethodLoginPage.addRequestHeader("Accept-Language", "en-us");
client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
initialState.addCookie(mycookie);

client.setState(initialState);
postMethodLoginPage.setRequestBody(logindata);
try {
status = client.executeMethod(postMethodLoginPage);
postMethodLoginPage.releaseConnection();
} catch (IOException ex1) {
}

//************Try to get the page after login**********************

GetMethod regetMethodInfoPage = new GetMethod("http://alta.registries.gov.ab.ca/spinii/legalnotice.aspx");

regetMethodInfoPage.addRequestHeader("Connection","Keep-Alive");

regetMethodInfoPage.addRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
regetMethodInfoPage.addRequestHeader("Accept-Encoding", "gzip, deflate");
regetMethodInfoPage.addRequestHeader("Accept-Language", "en-us");
client.getParams().setCookiePolicy(KookiePolicy.RFC_2109);
initialState.addKookie(mycookie);
client.setState(initialState);
try {
status = client.executeMethod(regetMethodInfoPage);
regetMethodInfoPage.releaseConnection();

System.out.println("\n Finished the conversation");
}
catch (IOException ex2) {
}
}
}