I’m making a POST request with login/pass and opening the site with an Intent. The site is opening but it is not logged in. What is wrong?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.co/login.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("pass", "mypass"));
nameValuePairs.add(new BasicNameValuePair("user", "myuser"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://mysite.com"));
startActivity(browserIntent);
The problems is that you are login into a website in your application. So, all cookies (which shows whether you are logged in or not) are stored in response.
You send new intent, which open a browser. However, browser doesn’t have access to these cookies which are saved in your response. So when it tries to access website, the website doesn’t get a cookie and thinks that you aren’t logged it.
I would say you should remove whole first part of this code and just have something like this:
This way the broswer will send authentication information and will receive a cookie in response and use it for any following communications.