Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > Software > Exporting Hotmail Address Book using HttpClient

Reply
Thread Tools

Exporting Hotmail Address Book using HttpClient

 
 
jcoder jcoder is offline
Junior Member
Join Date: Oct 2007
Posts: 2
 
      10-30-2007
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();
	}
}
 
Reply With Quote
 
 
 
 
jcoder jcoder is offline
Junior Member
Join Date: Oct 2007
Posts: 2
 
      10-30-2007
Sorry!!
its importing hotmail address book not exporting
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting the Outlook express address book to database Ashish ASP .Net 0 06-17-2005 11:02 AM
Importing Hotmail Addressbook from Java Client using commons-httpclient APIs Gamas Java 0 02-04-2005 11:50 PM
Exporting subgroup of OE address book Jim Beaver Computer Support 1 06-16-2004 11:31 PM
exporting address book Bigfred Computer Support 2 03-06-2004 05:50 PM
Exporting Outlook Express address book Jim Prather Computer Support 1 09-29-2003 02:54 PM



Advertisments