Exporting Hotmail Address Book using HttpClient

Discussion in 'Software' started by jcoder, Oct 30, 2007.

  1. jcoder

    jcoder

    Joined:
    Oct 30, 2007
    Messages:
    2
    Hi,

    I am using jakarta httpclient to export hotmail address book. For that i am loading the login form first and then retrieving dynamic parameters and then posting the form for authentication of user.
    I am unable to authenticate user after posting the form , it redirects me to the login page.
    Can somebody help me to figure out what i am missing in my code??

    Following is the code i have written so far:
    (I have even attached the java class and the debug/output files. To run java one will need to enter hotmail emailId and password in the loginData)

    Code:
    package msn.contacts.fetcher;
    
    import java.io.InputStream;
    
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.cyberneko.html.parsers.DOMParser;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    
    public class MSNContactsFetcher {
    	private String msnLoginUrl = "http://login.live.com/login.srf";
    		
    	public void login(){
    		HttpClient httpClient = new HttpClient();
    		httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    		int statusCode = -1;
    		InputStream responseStream = null;
    		DOMParser parser = null;
    		InputSource source = null;
    		
    		try{
    			GetMethod loadForm = new GetMethod(msnLoginUrl);
    			loadForm.setFollowRedirects(true);
    			statusCode = httpClient.executeMethod(loadForm);
    			
    			/*Header[] headers = loadForm.getResponseHeaders();
    			
    			for(int i= 0;i<headers.length;i++){
    				System.out.println("Header name:: " + headers[i].getName() + " value::: " + headers[i].getValue());
    			}*/
    			System.out.println("Hotmail Status Code::: " + statusCode );		
    			if(statusCode == HttpStatus.SC_OK){
    				System.out.println("Hotmail Login Form Loaded Successfully");
    				
    				System.out.println(loadForm.getResponseBodyAsString());
    				//System.exit(1);
    				String PPFT = null;
    				String PPSX = null;
    				String loginFormAction;
    				
    				parser = new DOMParser();
    				responseStream = loadForm.getResponseBodyAsStream();
    				source = new InputSource(responseStream);
    				parser.parse(source);
    				
    				Node form = parser.getDocument().getElementsByTagName("form").item(0);
    				NamedNodeMap attributes = form.getAttributes();
    				String formAction = attributes.getNamedItem("action").getNodeValue();
    				System.out.println("Form action ::: " +  formAction);
    				
    				NodeList inputElements = parser.getDocument().getElementsByTagName("input");
    				Node inputNode;
    				String name;
    
    				for(int i=0;i<inputElements.getLength();i++){
    					inputNode = inputElements.item(i);
    					name = inputNode.getAttributes().getNamedItem("name").getNodeValue();
    					//System.out.println(name + " " + inputNode.getAttributes().getNamedItem("value").getNodeValue() );
    					if(name.equals("PPFT")){
    						PPFT = inputNode.getAttributes().getNamedItem("value").getNodeValue();
    					}
    					else if(name.equals("PPSX")){
    						PPSX = inputNode.getAttributes().getNamedItem("value").getNodeValue();
    					}
    					else if(PPFT != null && PPSX!= null)
    						break;
    					else
    						continue;
    					
    				}
    				System.out.println("PPFT::: " + PPFT + " PPSX::: " + PPSX );
    				
    				NameValuePair[] loginData = {
    						new NameValuePair("PPSX",PPSX),
    						new NameValuePair("PwdPad","IfYouAreReadingThisYouHaveTooMuc"),
    						new NameValuePair("login","<emailId>"),
    						new NameValuePair("passwd","<password>"),
    						new NameValuePair("LoginOptions","3"),						
    						new NameValuePair("PPFT",PPFT)						
    				};
    				
    				PostMethod postLoginForm = new PostMethod(formAction);					
    				postLoginForm.setRequestBody(loginData);
    				httpClient.executeMethod(postLoginForm);
    				
    				statusCode = postLoginForm.getStatusCode();
    				System.out.println("Status Code::: " + statusCode);
    				
    				String responseString = postLoginForm.getResponseBodyAsString();
    				System.out.println("Response string:: " + responseString);
    			}
    		}catch(Exception ex){
    			System.out.println("Exception Occurred -->> " + ex.getMessage());
    			ex.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args) {
    		System.getProperties().setProperty("httpclient.useragent", " Mozilla/5.0 (Win98; I;Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4");
    		System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
    		System.setProperty("org.apache.commons.logging.simplelog.showdatetime","true");
    		System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header","debug");
    		MSNContactsFetcher msn = new MSNContactsFetcher();
    		msn.login();
    	}
    }
     
    jcoder, Oct 30, 2007
    #1
    1. Advertising

  2. jcoder

    jcoder

    Joined:
    Oct 30, 2007
    Messages:
    2
    Sorry!!
    its importing hotmail address book not exporting
     
    jcoder, Oct 30, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Prather

    Exporting Outlook Express address book

    Jim Prather, Sep 29, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    480
    Miggsee
    Sep 29, 2003
  2. Bigfred

    exporting address book

    Bigfred, Mar 1, 2004, in forum: Computer Support
    Replies:
    2
    Views:
    442
    Professor
    Mar 6, 2004
  3. Jim Beaver

    Exporting subgroup of OE address book

    Jim Beaver, Jun 16, 2004, in forum: Computer Support
    Replies:
    1
    Views:
    447
    °Mike°
    Jun 17, 2004
  4. fkissam

    Exporting all the Eudora Address Book Entries

    fkissam, Jul 8, 2004, in forum: Computer Support
    Replies:
    1
    Views:
    471
    °Mike°
    Jul 8, 2004
  5. Ken Houghton

    exporting my address book from microsoft outlook

    Ken Houghton, Aug 24, 2005, in forum: Computer Support
    Replies:
    1
    Views:
    487
    Paul Visser
    Aug 25, 2005
Loading...

Share This Page