Add cookie to default browser

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Add cookie to default browser

Postby paolo » Thu Nov 13, 2008 8:01 pm

Hi!
I wrote a small application that connects to a web server and exchange data via http (post), from the server properly receive the cookie, but when I open the default browser and I connect to the web server cookie is not found!
I try:

httpclient.getCookieStore().addCookie(myCookie);

but does not work.

Thanks,
Paolo
paolo
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 2:30 pm

Top

Postby Anen » Fri Nov 14, 2008 5:43 am

Hey paolo,

shall u show full of ur code for my concern! :idea: :) :)
A candle is not dimmed when lighting another candle.

ANEN..
Anen
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 7:48 am
Location: Canada

Postby paolo » Fri Nov 14, 2008 9:08 am

:D
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.IOException;
  2.  
  3. import java.util.List;
  4.  
  5.  
  6.  
  7. import org.apache.http.HttpEntity;
  8.  
  9. import org.apache.http.HttpResponse;
  10.  
  11. import org.apache.http.client.ClientProtocolException;
  12.  
  13. import org.apache.http.client.methods.HttpGet;
  14.  
  15. import org.apache.http.cookie.Cookie;
  16.  
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18.  
  19.  
  20.  
  21. import android.app.Activity;
  22.  
  23. import android.content.Intent;
  24.  
  25. import android.os.Bundle;
  26.  
  27.  
  28.  
  29. import android.text.Editable;
  30.  
  31. import android.util.Log;
  32.  
  33. import android.view.View;
  34.  
  35. import android.view.View.OnClickListener;
  36.  
  37. import android.widget.EditText;
  38.  
  39. import android.widget.SeekBar;
  40.  
  41. import android.widget.TextView;
  42.  
  43. import android.widget.ToggleButton;
  44.  
  45.  
  46.  
  47. public class LocalServiceController extends Activity {
  48.  
  49.  
  50.  
  51.         private Intent i;
  52.  
  53.         private ToggleButton button;
  54.  
  55.         private SeekBar mSeekBar;
  56.  
  57.         private TextView mProgressText;
  58.  
  59.         private static EditText serverText,userText;
  60.  
  61.     @Override
  62.  
  63.     protected void onCreate(Bundle savedInstanceState) {
  64.  
  65.         super.onCreate(savedInstanceState);
  66.  
  67.        
  68.  
  69.         setContentView(R.layout.main);
  70.  
  71.  
  72.  
  73.         i=new Intent(LocalServiceController.this, LocalService.class);
  74.  
  75.         // Watch for button clicks.
  76.  
  77.         button = (ToggleButton)findViewById(R.id.toggle);
  78.  
  79.         button.setOnClickListener(mStartListener);
  80.  
  81.         button.setChecked(LocalService.getStato());
  82.  
  83.        
  84.  
  85.         mSeekBar = (SeekBar)findViewById(R.id.seek);
  86.  
  87.         mProgressText = (TextView)findViewById(R.id.progress);
  88.  
  89.         mProgressText.setText(LocalService.getSleep()/1000 + " seconds");
  90.  
  91.         mSeekBar.setProgress((LocalService.getSleep()/1000)-1);
  92.  
  93.        
  94.  
  95.         serverText = (EditText)findViewById(R.id.servertext);
  96.  
  97.         userText = (EditText)findViewById(R.id.usertext);
  98.  
  99.         checkCookie();
  100.  
  101.     }
  102.  
  103.     @Override
  104.  
  105.     public void  onDestroy() {
  106.  
  107.         super.onDestroy();
  108.  
  109.     }
  110.  
  111.     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
  112.  
  113.         LocalService.setSleep((progress+1)*1000);
  114.  
  115.         mProgressText.setText(LocalService.getSleep()/1000 + " seconds");
  116.  
  117.     }
  118.  
  119.  
  120.  
  121.     private OnClickListener mStartListener = new OnClickListener() {
  122.  
  123.         public void onClick(View v) {
  124.  
  125.                 if(!LocalService.getStato()){
  126.  
  127.                         startService(i);
  128.  
  129.                         button.setChecked(true);
  130.  
  131.                         }
  132.  
  133.                 else {
  134.  
  135.                         stopService(i);
  136.  
  137.                         button.setChecked(false);
  138.  
  139.                         }
  140.  
  141.         }
  142.  
  143.     };
  144.  
  145.     public static Editable getServer(){
  146.  
  147.         return serverText.getText();
  148.  
  149.     }
  150.  
  151.     public static Editable getUser(){
  152.  
  153.         return userText.getText();
  154.  
  155.     }
  156.  
  157.     private void checkCookie(){
  158.  
  159.         DefaultHttpClient httpclient = new DefaultHttpClient();
  160.  
  161.         HttpResponse response;
  162.  
  163.         try {
  164.  
  165.                 // Get cookies from the login page (not the address same of the form post)
  166.  
  167.                  HttpGet httpget = new HttpGet("http://"+LocalServiceController.getServer()+"/Search/cookie.jsp?user="+LocalServiceController.getUser());
  168.  
  169.        
  170.  
  171.                 response = httpclient.execute(httpget);
  172.  
  173.                 HttpEntity entity = response.getEntity();
  174.  
  175.                
  176.  
  177.                  Log.d("httpPost", "Login form get: " + response.getStatusLine());
  178.  
  179.                  if (entity != null) {
  180.  
  181.                      entity.consumeContent();
  182.  
  183.                  }
  184.  
  185.                  
  186.  
  187.                  Log.d("httpPost", "Initial set of cookies:");
  188.  
  189.                  List<Cookie> cookies = httpclient.getCookieStore().getCookies();
  190.  
  191.                  if (cookies.isEmpty()) {
  192.  
  193.                       Log.d("httpPost", "None");
  194.  
  195.                  } else {
  196.  
  197.                      for (int i = 0; i < cookies.size(); i++) {
  198.  
  199.                       Log.d("httpPost", "- " + cookies.get(i).toString());
  200.  
  201.                       httpclient.getCookieStore().addCookie(cookies.get(i));
  202.  
  203.                      }
  204.  
  205.                  }
  206.  
  207.         } catch (ClientProtocolException e) {
  208.  
  209.             Log.e("httpPost", e.getMessage());
  210.  
  211.             e.printStackTrace();
  212.  
  213.        } catch (IOException e) {
  214.  
  215.             Log.e("httpPost", e.getMessage());
  216.  
  217.             e.printStackTrace();
  218.  
  219.        } catch (Exception e) {
  220.  
  221.             Log.e("httpPost", e.getMessage());
  222.  
  223.             e.printStackTrace();
  224.  
  225.        }    
  226.  
  227.     }
  228.  
  229. }
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


is enough?
Thanks!
paolo
Freshman
Freshman
 
Posts: 4
Joined: Thu Nov 13, 2008 2:30 pm

Postby ismarc » Tue Nov 18, 2008 12:41 am

Based on the code you posted, you are performing a GET, not a POST. Either way, does your log output show any cookies as being found? Something to note is that your DefaultHttpClient doesn't exist after the function exits. That means any assigned values will disappear. You would need to store the cookies in a member variable and assign those cookies when you try to use a different client. If you aren't getting any cookies, then obviously you can't assign them.
ismarc
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 29, 2008 6:09 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests