G
Gamas
Hi guys,
I am trying to get Hotmail Addressbook Page from my Java App, but
haven't been succesfull.
I am using Jakarta commons-httpclient APIs, JDK1.4.2 to do so. Any help
would be greatly appreciated. Thank you very much.
Below is my code:
public void doImport(String sLogin, String sPassword) throws
Exception {
InputStream result = null;
if(client == null) {
client = new HttpClient();
}
client.getHostConfiguration().setHost("login.passport.com",
443, "https");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod authpost = new PostMethod("/ppsecure/post.srf");
NameValuePair action = new NameValuePair("sec", "true");
NameValuePair userid = new NameValuePair("login", sLogin);
NameValuePair password = new NameValuePair("passwd",
sPassword);
//authpost.setRequestBody(new NameValuePair[] {action, userid,
password,ls,id,cbid,da,mspp_shared});
authpost.setRequestBody(new NameValuePair[] {action, userid,
password});
int status = client.executeMethod(authpost);
System.out.println("\nLogin form post: " +
authpost.getStatusLine().toString());
System.out.println("\nbody=\n"+authpost.getResponseBodyAsString());
CookieSpec cookiespec =
CookiePolicy.getCookieSpec(CookiePolicy.BROWSER_COMPATIBILITY);
Cookie[] logoncookies = cookiespec.match(
"login.passport.com", 443, "/ppsecure/post.srf", true,
client.getState().getCookies());
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
vErrorMessages.add("login invalid");
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies.toString());
}
}
// release any connection resources used by the method
authpost.releaseConnection();
// Usually a successful form-based login results in a redicrect
to
// another url
statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
client.getHostConfiguration().setHost("hotmail.msn.com", 80, "http");
GetMethod redirect = new
GetMethod("/cgi-bin/addresses");
redirect.setFollowRedirects(true);
client.executeMethod(redirect);
System.out.println("Redirect: " +
redirect.getStatusLine().toString());
result = redirect.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(result));
StringBuffer sbResult = new StringBuffer();
int r = 0;
while((r = reader.read()) != -1) {
sbResult.append((char)r);
}
reader.close();
addressBook = sbResult.toString();
redirect.releaseConnection();
} else {
vErrorMessages.add("invalid redirect");
System.out.println("Invalid redirect");
}
}
}
This is what I get in my console:
Login form post: HTTP/1.1 200 OK
body=
<HTML><HEAD><meta HTTP-EQUIV="Content-Type" content="text/html;
charset=iso-8859-1"><META HTTP-EQUIV="REFRESH" CONTENT="0;
URL=http://login.passport.net/UICookiesDisabled.srf?rollrs=11"><script>function
OnBack(){}</script></HEAD></HTML>
Logon cookies:
None
I am trying to get Hotmail Addressbook Page from my Java App, but
haven't been succesfull.
I am using Jakarta commons-httpclient APIs, JDK1.4.2 to do so. Any help
would be greatly appreciated. Thank you very much.
Below is my code:
public void doImport(String sLogin, String sPassword) throws
Exception {
InputStream result = null;
if(client == null) {
client = new HttpClient();
}
client.getHostConfiguration().setHost("login.passport.com",
443, "https");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod authpost = new PostMethod("/ppsecure/post.srf");
NameValuePair action = new NameValuePair("sec", "true");
NameValuePair userid = new NameValuePair("login", sLogin);
NameValuePair password = new NameValuePair("passwd",
sPassword);
//authpost.setRequestBody(new NameValuePair[] {action, userid,
password,ls,id,cbid,da,mspp_shared});
authpost.setRequestBody(new NameValuePair[] {action, userid,
password});
int status = client.executeMethod(authpost);
System.out.println("\nLogin form post: " +
authpost.getStatusLine().toString());
System.out.println("\nbody=\n"+authpost.getResponseBodyAsString());
CookieSpec cookiespec =
CookiePolicy.getCookieSpec(CookiePolicy.BROWSER_COMPATIBILITY);
Cookie[] logoncookies = cookiespec.match(
"login.passport.com", 443, "/ppsecure/post.srf", true,
client.getState().getCookies());
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
vErrorMessages.add("login invalid");
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies.toString());
}
}
// release any connection resources used by the method
authpost.releaseConnection();
// Usually a successful form-based login results in a redicrect
to
// another url
statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
client.getHostConfiguration().setHost("hotmail.msn.com", 80, "http");
GetMethod redirect = new
GetMethod("/cgi-bin/addresses");
redirect.setFollowRedirects(true);
client.executeMethod(redirect);
System.out.println("Redirect: " +
redirect.getStatusLine().toString());
result = redirect.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(result));
StringBuffer sbResult = new StringBuffer();
int r = 0;
while((r = reader.read()) != -1) {
sbResult.append((char)r);
}
reader.close();
addressBook = sbResult.toString();
redirect.releaseConnection();
} else {
vErrorMessages.add("invalid redirect");
System.out.println("Invalid redirect");
}
}
}
This is what I get in my console:
Login form post: HTTP/1.1 200 OK
body=
<HTML><HEAD><meta HTTP-EQUIV="Content-Type" content="text/html;
charset=iso-8859-1"><META HTTP-EQUIV="REFRESH" CONTENT="0;
URL=http://login.passport.net/UICookiesDisabled.srf?rollrs=11"><script>function
OnBack(){}</script></HEAD></HTML>
Logon cookies:
None