How to login to ASP.net 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 ASP.net page through apache HTTPClient

Threaded View

  1. #1
    Join Date
    Dec 2008
    Posts
    2

    How to login to ASP.net page through apache HTTPClient

    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.
    Code:
    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.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));
           Cookie mycookie = new Cookie(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(CookiePolicy.RFC_2109);
    initialState.addCookie(mycookie);
    client.setState(initialState);
           try {
              status = client.executeMethod(regetMethodInfoPage);
            regetMethodInfoPage.releaseConnection();
    
             System.out.println("\n Finished the conversation");
           }
            catch (IOException ex2) {
          }
      }
    }
    Last edited by Hack; 12-12-2008 at 08:28 AM. Reason: Added Code Tags

Similar Threads

  1. font size changed on asp.net page
    By dootam in forum ASP.NET
    Replies: 3
    Last Post: 01-04-2009, 11:06 PM
  2. Replies: 2
    Last Post: 04-18-2007, 03:34 AM
  3. prob.. in login page
    By abhit_kumar in forum Java
    Replies: 0
    Last Post: 02-24-2005, 09:49 AM
  4. Treeview control for my ASP.Net web page
    By Franco22 in forum Architecture and Design
    Replies: 1
    Last Post: 10-18-2002, 12:12 AM
  5. Calling dll's from ASP.Net
    By Jon Gregory Rothlander in forum ASP.NET
    Replies: 2
    Last Post: 06-20-2001, 02: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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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