I am trying to make an app which logs into a website and extracts some info. I am using the DefaultHttpClient however i make my post request but it doesnt seem to remember my username + password once im signed in - so i think its not storing cookies automatically.
Below is a code snippet of my app - can anybody please tel me how I enable it to store cookies automatically please!
Thanks
Andy
Using java Syntax Highlighting
- public void formLogin(String url) throws ClientProtocolException, IOException{
- localContext = new BasicHttpContext();
- Thread t = new Thread(){
- public void run(){
- try{
- httpclient = new DefaultHttpClient();
- // httpclient.addResponseInterceptor( new ResponseProcessCookies() );
- // httpclient.addRequestInterceptor( new RequestAddCookies() );
- List<Cookie> cookies = httpclient.getCookieStore().getCookies();
- httpclient.getParams().setParameter(
- "http.useragent",
- "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
- );
- HttpGet httpget = new HttpGet("http://wap.cokezone.co.uk");
- HttpResponse response = httpclient.execute(httpget,localContext);
- HttpEntity entity = response.getEntity();
- sendStatusUpdate("Connecting to CokeZone...",0);
- String homepage = printPage(entity.getContent());
- Scanner scanner = new Scanner(homepage);
- String temp ;
- String login = null;
- while(scanner.hasNext()){
- temp = scanner.nextLine();
- if(temp.contains("Log")){
- login = temp.split("\"")[7];
- }
- }
- HttpGet httpgetLogin = new HttpGet(login);
- response = httpclient.execute(httpgetLogin,localContext);
- entity = response.getEntity();
- String loginPage = printPage(entity.getContent());
- sendStatusUpdate("Starting login to CokeZone...",0);
- scanner = new Scanner(loginPage);
- login = null;
- while(scanner.hasNext()){
- temp = scanner.nextLine();
- if(temp.contains("form action")){
- login = temp.split("\"")[1];
- }
- }
- //Got LOGIN POST to create:
- HttpPost httpost = new HttpPost(login);
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- nvps.add(new BasicNameValuePair("email", "renegadeandy@gmail.com"));
- nvps.add(new BasicNameValuePair("password","password"));
- nvps.add(new BasicNameValuePair("go", ",+Login"));
- httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
- sendStatusUpdate("Logging into CokeZone...",0);
- response = httpclient.execute(httpost,localContext);
- entity = response.getEntity();
- httpget = new HttpGet(login);
- response = httpclient.execute(httpget,localContext);
- entity = response.getEntity();
- //
- TextView points = (TextView)findViewById(R.id.points);
- loggedIn = printPage(entity.getContent());
- String secondSplit = loggedIn.split("You have")[1];
- String pointsFound = secondSplit.split(" ")[1];
- points.setText(pointsFound);
- sendStatusUpdate("Logged into CokeZone!",0);
- if (cookies.isEmpty()) { // didn't get cookies
- Log.e("debug","no cookies");
- }
- else{
- Log.e("debug","hmmmm cookies");
- }
- //ok so now logged in
- // httpclient.getConnectionManager().shutdown();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- };
- t.start();
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4

