HttpClient which automatically handles cookies

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

HttpClient which automatically handles cookies

Postby renegadeandy » Sat Apr 03, 2010 1:58 pm

Hey everyone,

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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public void formLogin(String url) throws ClientProtocolException, IOException{
  2.  
  3.         localContext = new BasicHttpContext();
  4.  
  5.    
  6.  
  7.         Thread t = new Thread(){
  8.  
  9.                
  10.  
  11.                 public void run(){
  12.  
  13.                         try{
  14.  
  15.                                
  16.  
  17.      httpclient = new DefaultHttpClient();
  18.  
  19.    // httpclient.addResponseInterceptor( new ResponseProcessCookies() );
  20.  
  21.    // httpclient.addRequestInterceptor( new RequestAddCookies() );
  22.  
  23.    
  24.  
  25. List<Cookie> cookies = httpclient.getCookieStore().getCookies();
  26.  
  27.    
  28.  
  29.  
  30.  
  31.         httpclient.getParams().setParameter(
  32.  
  33.                         "http.useragent",
  34.  
  35.                     "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
  36.  
  37.                 );
  38.  
  39.  
  40.  
  41.         HttpGet httpget = new HttpGet("http://wap.cokezone.co.uk");
  42.  
  43.  
  44.  
  45.         HttpResponse response = httpclient.execute(httpget,localContext);
  46.  
  47.         HttpEntity entity = response.getEntity();
  48.  
  49.         sendStatusUpdate("Connecting to CokeZone...",0);
  50.  
  51.         String homepage = printPage(entity.getContent());
  52.  
  53.        
  54.  
  55.         Scanner scanner = new Scanner(homepage);
  56.  
  57.         String temp ;
  58.  
  59.         String login = null;
  60.  
  61.        while(scanner.hasNext()){
  62.  
  63.            temp = scanner.nextLine();
  64.  
  65.            if(temp.contains("Log")){
  66.  
  67.                    login = temp.split("\"")[7];
  68.  
  69.            }
  70.  
  71.        }
  72.  
  73.  
  74.  
  75.        HttpGet httpgetLogin = new HttpGet(login);
  76.  
  77.        response = httpclient.execute(httpgetLogin,localContext);
  78.  
  79.        entity = response.getEntity();
  80.  
  81.  
  82.  
  83.            
  84.  
  85.            
  86.  
  87.        String loginPage = printPage(entity.getContent());
  88.  
  89.        sendStatusUpdate("Starting login to CokeZone...",0);
  90.  
  91.         scanner = new Scanner(loginPage);
  92.  
  93.        
  94.  
  95.        login = null;
  96.  
  97.       while(scanner.hasNext()){
  98.  
  99.            temp = scanner.nextLine();
  100.  
  101.            if(temp.contains("form action")){
  102.  
  103.                    login = temp.split("\"")[1];
  104.  
  105.            }
  106.  
  107.       }
  108.  
  109.        
  110.  
  111.      
  112.  
  113.    
  114.  
  115.        //Got LOGIN POST to create:
  116.  
  117.        
  118.  
  119.         HttpPost httpost = new HttpPost(login);
  120.  
  121.  
  122.  
  123.         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  124.  
  125.         nvps.add(new BasicNameValuePair("email", "renegadeandy@gmail.com"));
  126.  
  127.         nvps.add(new BasicNameValuePair("password","password"));
  128.  
  129.         nvps.add(new BasicNameValuePair("go", ",+Login"));
  130.  
  131.      
  132.  
  133.         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  134.  
  135.         sendStatusUpdate("Logging into CokeZone...",0);
  136.  
  137.         response = httpclient.execute(httpost,localContext);
  138.  
  139.         entity = response.getEntity();
  140.  
  141.  
  142.  
  143.          
  144.  
  145.         httpget = new HttpGet(login);
  146.  
  147.  
  148.  
  149.         response = httpclient.execute(httpget,localContext);
  150.  
  151.         entity = response.getEntity();
  152.  
  153.       //
  154.  
  155.       TextView points = (TextView)findViewById(R.id.points);
  156.  
  157.       loggedIn = printPage(entity.getContent());
  158.  
  159.      
  160.  
  161. String secondSplit = loggedIn.split("You have")[1];
  162.  
  163.    
  164.  
  165. String pointsFound =    secondSplit.split(" ")[1];
  166.  
  167.  
  168.  
  169. points.setText(pointsFound);
  170.  
  171. sendStatusUpdate("Logged into CokeZone!",0);
  172.  
  173. if (cookies.isEmpty()) {    // didn't get cookies
  174.  
  175.         Log.e("debug","no cookies");
  176.  
  177.         }  
  178.  
  179. else{
  180.  
  181.         Log.e("debug","hmmmm cookies");
  182.  
  183. }
  184.  
  185.        
  186.  
  187.         //ok so now logged in
  188.  
  189.       //  httpclient.getConnectionManager().shutdown();  
  190.  
  191.        
  192.  
  193.                         }
  194.  
  195.                         catch(Exception e){
  196.  
  197.                                
  198.  
  199.                                 e.printStackTrace();
  200.  
  201.                         }
  202.  
  203.                 }
  204.  
  205.         };
  206.  
  207.        
  208.  
  209.        
  210.  
  211.         t.start();
  212.  
  213.         // When HttpClient instance is no longer needed,
  214.  
  215.         // shut down the connection manager to ensure
  216.  
  217.         // immediate deallocation of all system resources
  218.  
  219.  
  220.  
  221.        
  222.  
  223.              
  224.  
  225.     }
  226.  
  227.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
renegadeandy
Freshman
Freshman
 
Posts: 6
Joined: Mon Jun 22, 2009 7:29 pm

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests