You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Christopher Valorose <cv...@yahoo.com> on 2005/11/09 19:31:09 UTC
Using HTTPClient to login to Yahoo Briefcase
Hello,
I am attempting to use HTTPClient to login into Yahoo.
No matter what I do I keep getting the Yahoo logon
page again. See code below. Any examples or help is
greatly appreciated.
public boolean formLoginYahoo( String user, String
password){
boolean bFormLogin = false;
String url = "https://login.yahoo.com";
int port = 443;
_client = new HttpClient();
_client.getHostConfiguration().setHost(url,
port, "https");
_client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
GetMethod authget = new GetMethod(url);
try{
_client.executeMethod(authget);
}
catch (IOException i){
i.printStackTrace();
}
System.out.println("Login form get: " +
authget.getStatusLine().toString());
// Read the response body.
byte[] responseBody = authget.getResponseBody();
_strGetRspBody = authget.getResponseBodyAsString();
_logger.logDebug("GetRspBody: " + _strGetRspBody);
// release any connection resources used by the
method
authget.releaseConnection();
//Get the .u value
int intUStart = _strGetRspBody.indexOf("<input
type=\"hidden\" name=\".u\"");
intUStart = intUStart + 38;
String strU = _strGetRspBody.substring(intUStart,
intUStart + 13);
_logger.logDebug("U value from Get: " + strU);
//Get the .challenge value
int intChallengeStart =
_strGetRspBody.indexOf("<input type=\"hidden\"
name=\".challenge\"");
intChallengeStart = intChallengeStart + 46;
String strChallenge =
_strGetRspBody.substring(intChallengeStart,
intChallengeStart + 28);
_logger.logDebug("Challenge value from Get: " +
strChallenge);
NameValuePair tries = new NameValuePair("action",
"login");
NameValuePair src = new NameValuePair("url",
"/index.html");
NameValuePair md5 = new NameValuePair(".md5", "");
NameValuePair hash = new NameValuePair(".hash",
"");
NameValuePair js = new NameValuePair(".js", "");
NameValuePair last = new NameValuePair(".last",
"");
NameValuePair promo = new NameValuePair("promo",
"");
NameValuePair intl = new NameValuePair(".intl",
"us");
NameValuePair bypass = new NameValuePair(".bypass",
"");
NameValuePair partner = new
NameValuePair(".partner", "");
NameValuePair u = new NameValuePair(".u", strU);
NameValuePair v = new NameValuePair(".v", "0");
NameValuePair challenge = new
NameValuePair(".challenge", strChallenge);
NameValuePair yplus = new NameValuePair(".yplus",
"");
NameValuePair emailCode = new
NameValuePair(".emailCode", "");
NameValuePair pkg = new NameValuePair("pkg", "");
NameValuePair stepid = new NameValuePair("stepid",
"");
NameValuePair ev = new NameValuePair(".ev", "");
NameValuePair hasMsgr = new NameValuePair("hasMsgr",
"0");
NameValuePair chkP = new NameValuePair(".chkP",
"Y");
NameValuePair done = new NameValuePair(".done",
"https://my.secure.yahoo.com");
NameValuePair username = new
NameValuePair("username", user);
NameValuePair passwd = new
NameValuePair("passwd", password);
NameValuePair[] valuePairs = {tries, src, md5, hash,
js, last, promo, intl,
bypass, partner, u, v, challenge, yplus,
emailCode,
pkg, stepid, ev, hasMsgr, chkP, done,
username, passwd};
String strLogonUrl =
"https://login.yahoo.com/config/login?";
PostMethod authpost = new
PostMethod(strLogonUrl);
// Prepare login parameters
authpost.setRequestBody( valuePairs );
try{
_client.executeMethod(authpost);
}
catch (IOException i){
i.printStackTrace();
}
String strStatusLine =
authpost.getStatusLine().toString();
System.out.println("Login form post: " +
strStatusLine);
_strPostRspBody =
authpost.getResponseBodyAsString();
_logger.logDebug("Response Body from Post: \n" +
_strPostRspBody);
// release any connection resources used by
the method
authpost.releaseConnection();
if (strStatusLine.equals(_success1_1))
bFormLogin = true;
// See if we got any cookies
// The only way of telling whether logon
succeeded is
// by finding a session cookie
Cookie[] logoncookies =
_client.getState().getCookies(url, port, "/", false);
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length;
i++) {
System.out.println("- " +
logoncookies[i].toString());
}
}
// Usually a successful form-based login
results in a redicrect to
// another url
int 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);
GetMethod redirect = new
GetMethod(newuri);
try{
_client.executeMethod(redirect);
}
catch (IOException i){
i.printStackTrace();
}
System.out.println("Redirect: " +
redirect.getStatusLine().toString());
// release any connection resources
used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid
redirect");
System.exit(1);
}
}
return bFormLogin;
}// end class
Thanks in advance
Chris
Thanks
Christophr Valorose
Office: 757-249-7174
Home: 757-565-0883
Cell: 757-784-0789
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org