How to login to page through apache HTTPClient

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to login to page through apache HTTPClient

  1. #1
    Join Date
    Dec 2008

    How to login to page through apache HTTPClient


    I am trying to login to 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.Cookie;
    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.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.*;
    public class Test {
    static final String hostURI = "";
    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 = "";
    // 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
         List authPrefs = new ArrayList(3);
         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().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));
           Cookie mycookie = new Cookie(hostURI, "mycookie", "stuff", "/", null, false);
           GetMethod getMethodInfoPage = new GetMethod(LOGON_SITE);
            getMethodInfoPage.addRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/, application/, 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");
             System.out.println("End of getting Initial Page"); // there may not be a HEAD tag
            catch (IOException ex2) {
            catch (ParserException ex2) {
        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(""); 
        postMethodLoginPage.addRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/, application/, application/msword, */*");
        postMethodLoginPage.addRequestHeader("Accept-Encoding", "gzip, deflate");
        postMethodLoginPage.addRequestHeader("Accept-Language", "en-us");
    try {
      status = client.executeMethod(postMethodLoginPage);
    } catch (IOException ex1) {
    //************Try to get the page after login**********************
             GetMethod regetMethodInfoPage  = new GetMethod("");
             regetMethodInfoPage.addRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/, application/, application/msword, */*");
             regetMethodInfoPage.addRequestHeader("Accept-Encoding", "gzip, deflate");
             regetMethodInfoPage.addRequestHeader("Accept-Language", "en-us");
           try {
              status = client.executeMethod(regetMethodInfoPage);
             System.out.println("\n Finished the conversation");
            catch (IOException ex2) {
    Last edited by Hack; 12-12-2008 at 07:28 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    Welcome to DevX

    What is the exception error that you are getting?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Dec 2008
    Hi Hack,

    Thank you for ur reply. I am getting following msgs. The problem is that I can't get the legal notice page after login. It giving me the same login page. The web suppose to redirect me to legal notice page after login.

    About to make call for getMethodInfoPage connection atte

    Reply from initial get to info page
    getMethodInfoPage 200 with status text OK
    End of getting Initial Page

    Start posting data to
    Header : [Lorg.apache.commons.httpclient.Header;@18dfef8
    Date : Fri, 12 Dec 2008 16:17:10 GMT
    Server : Microsoft-IIS/6.0
    X-Powered-By : ASP.NET
    X-AspNet-Version : 1.1.4322
    Cache-Control : private
    Content-Type : text/html; charset=utf-8
    Content-Length : 20117

    Reply from post to login page

    postMethodLoginPage status 200 with status text OK

    postMethodLoginPage AuthState Auth state: auth requested

    End posting Data

    Get the search page will post to http://alta.registries.
    Initial set of cookies:
    Header : [Lorg.apache.commons.httpclient.Header;@d6c16c
    Date : Fri, 12 Dec 2008 16:17:10 GMT
    Server : Microsoft-IIS/6.0
    X-Powered-By : ASP.NET
    X-AspNet-Version : 1.1.4322
    Cache-Control : private
    Content-Type : text/html; charset=utf-8
    Content-Length : 20117
    Path : /spinii/logon.aspx

    regetMethodInfoPage status 200 with status text OK

    Finished the conversation

Similar Threads

  1. font size changed on page
    By dootam in forum ASP.NET
    Replies: 3
    Last Post: 01-04-2009, 10:06 PM
  2. Replies: 2
    Last Post: 04-18-2007, 02:34 AM
  3. prob.. in login page
    By abhit_kumar in forum Java
    Replies: 0
    Last Post: 02-24-2005, 08:49 AM
  4. Treeview control for my ASP.Net web page
    By Franco22 in forum Architecture and Design
    Replies: 1
    Last Post: 10-17-2002, 11:12 PM
  5. Calling dll's from ASP.Net
    By Jon Gregory Rothlander in forum ASP.NET
    Replies: 2
    Last Post: 06-20-2001, 01:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center