publicstaticMyBean gatherData(String username,String password){
MyBean myBean =newMyBean();
try{
HttpResponse response = doHttpGet(URL_PAGE_LOGIN,null,null);
System.out.println("Got login page");
String content =EntityUtils.toString(response.getEntity());
String token =ContentParser.getToken(content);
String cookie = getCookie(response);
System.out.println("Performing login");
System.out.println("token = "+token +" || cookie = "+cookie);
response = doLoginPost(username,password,cookie, token);
int respCode = response.getStatusLine().getStatusCode();
if(respCode !=302){
System.out.println("ERROR: not a 302 redirect!: code is \""+ respCode+"\"");
if(respCode ==200){
System.out.println(getHeaders(response));
System.out.println(EntityUtils.toString(response.getEntity()).substring(0,500));
}
}else{
System.out.println("Logged in OK, loading account home");
// redirect handler and rest of parse removed
}
}catch(Exception e){
System.out.println("ERROR in gatherdata: "+e.toString());
e.printStackTrace();
}
return myBean;
}
privatestaticHttpResponse doHttpGet(String url,String cookie,String referrer){
try{
HttpClient client =newDefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");
HttpGet httpGet =newHttpGet(url);
httpGet.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
httpGet.setHeader(HEADER_USER_AGENT,HEADER_USER_AGENT_VALUE);
if(referrer !=null&&!referrer.equals("")) httpGet.setHeader(HEADER_REFERER,referrer);
if(cookie !=null&&!cookie.equals("")) httpGet.setHeader(HEADER_COOKIE,cookie);
return client.execute(httpGet);
}catch(Exception e){
e.printStackTrace();
thrownewConnectException("Failed to read content from response");
}
}
privatestaticHttpResponse doLoginPost(String username,String password,String cookie,String token)throwsClientProtocolException,IOException{
try{
HttpClient client =newDefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");
HttpPost post =newHttpPost(URL_LOGIN_SUBMIT);
post.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
post.setHeader(HEADER_USER_AGENT,HEADER_USER_AGENT_VALUE);
post.setHeader(HEADER_REFERER, URL_PAGE_LOGIN);
post.setHeader(HEADER_COOKIE, cookie);
post.setHeader("Content-Type","application/x-www-form-urlencoded");
List<NameValuePair> formParams =newArrayList<NameValuePair>();
formParams.add(newBasicNameValuePair("org.apache.struts.taglib.html.TOKEN", token));
formParams.add(newBasicNameValuePair("showLogin","true"));
formParams.add(newBasicNameValuePair("upgrade",""));
formParams.add(newBasicNameValuePair("username", username));
formParams.add(newBasicNameValuePair("password", password));
formParams.add(newBasicNameValuePair("submit","Secure+Log+in"));
UrlEncodedFormEntity entity =newUrlEncodedFormEntity(formParams,HTTP.UTF_8);
post.setEntity(entity);
return client.execute(post);
}catch(Exception e){
e.printStackTrace();
thrownewConnectException("ERROR in doLoginPost(): "+e.getMessage());
}
}