Login to a https (secured)website from android application

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

Login to a https (secured)website from android application

Postby anurahim » Thu Oct 30, 2008 2:51 pm

Hi frnds,
my application needs to login to a web site thru the application. How
can i do it?
for that I need to post the username ,password and some other data to
the url.
the code is as follows. I am using SDK 1.0
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("my url to submit");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user_name", "myusername"));
nvps.add(new BasicNameValuePair("password", "mypassword"));
nvps.add(new BasicNameValuePair("hidden_data_to_post", "data
to post"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
if I execute this code , I will get an error:
Exception in thread "main" java.lang.RuntimeException: Stub!
with error in the line
DefaultHttpClient httpclient = new DefaultHttpClient();

if I use
HttpClient client = new HttpClient();
try{
method = new PostMethod("https://my url to post");
method.addParameter("user_name", "myusername"));
method.addParameter("password", "mypassword"));
method.addParameter("hidden_data_to_post", "data to post"));
method.setFollowRedirects(false);
client.executeMethod(method);
the response body will be 405 error
if I set the
method.setFollowRedirects(false); to be true,
the error will be
java.lang.IllegalArgumentException: Entity enclosing requests cannot be redirected without user intervention

So what may be the probelm.If I am wrng pls help me with sample code to login to https site with login data and some other parametrs the server needs using SDK 1.0
anur
anurahim
Junior Developer
Junior Developer
 
Posts: 24
Joined: Fri Jun 27, 2008 11:26 am

Top

Postby dave » Fri Oct 31, 2008 5:30 pm

I'm also having the same problem. Except I'm logging into a rails app.

Hope someone can help out.

Cheers

Dave
dave
Junior Developer
Junior Developer
 
Posts: 11
Joined: Fri Oct 31, 2008 5:29 pm
Location: Kent

Re: Login to a https (secured)website from android applicati

Postby ismarc » Fri Oct 31, 2008 8:44 pm

anurahim wrote:Hi frnds,
my application needs to login to a web site thru the application. How
can i do it?
for that I need to post the username ,password and some other data to
the url.
the code is as follows. I am using SDK 1.0
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("my url to submit");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user_name", "myusername"));
nvps.add(new BasicNameValuePair("password", "mypassword"));
nvps.add(new BasicNameValuePair("hidden_data_to_post", "data
to post"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
if I execute this code , I will get an error:
Exception in thread "main" java.lang.RuntimeException: Stub!
with error in the line
DefaultHttpClient httpclient = new DefaultHttpClient();

if I use
HttpClient client = new HttpClient();
try{
method = new PostMethod("https://my url to post");
method.addParameter("user_name", "myusername"));
method.addParameter("password", "mypassword"));
method.addParameter("hidden_data_to_post", "data to post"));
method.setFollowRedirects(false);
client.executeMethod(method);
the response body will be 405 error
if I set the
method.setFollowRedirects(false); to be true,
the error will be
java.lang.IllegalArgumentException: Entity enclosing requests cannot be redirected without user intervention

So what may be the probelm.If I am wrng pls help me with sample code to login to https site with login data and some other parametrs the server needs using SDK 1.0


It's a problem with your parameters. From Wikipedia's entry on HTTP status codes:
405 Method Not Allowed
A request was made of a resource using a request method not supported by that resource; for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.


The method I use for posting data right now for what I've been working on is:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         DefaultHttpClient httpclient = new DefaultHttpClient();
  3.  
  4.         HttpPost httppost = new HttpPost(url + authPath);
  5.  
  6.         ArrayList<BasicNameValuePair> pairs = new ArrayList();
  7.  
  8.         pairs.add(new BasicNameValuePair("username", logonName));
  9.  
  10.         pairs.add(new BasicNameValuePair("password", this.password));
  11.  
  12.         pairs.add(new BasicNameValuePair("destination", this.url + "/Exchange/"));
  13.  
  14.         pairs.add(new BasicNameValuePair("flags", "0"));
  15.  
  16.         pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
  17.  
  18.         pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
  19.  
  20.         pairs.add(new BasicNameValuePair("trusted", "0"));
  21.  
  22.         try {
  23.  
  24.             UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs);
  25.  
  26.        
  27.  
  28.             /** Assign the POST data to the entity */
  29.  
  30.             httppost.setEntity(p_entity);
  31.  
  32.  
  33.  
  34.             /** Perform the actual HTTP POST */
  35.  
  36.             HttpResponse response = httpclient.execute(httppost);
  37.  
  38.             HttpEntity entity = response.getEntity();
  39.  
  40.             int status_code = response.getStatusLine().getStatusCode();
  41.  
  42.             this.cookies = httpclient.getCookieStore();
  43.  
  44.             if (status_code >= 300) {
  45.  
  46.                 this.isAuthenticated = false;
  47.  
  48.                 return false;
  49.  
  50.             } else {
  51.  
  52.                 this.isAuthenticated = true;
  53.  
  54.                 return true;
  55.  
  56.             }
  57.  
  58.         } catch (UnsupportedEncodingException uee) {
  59.  
  60.             // Woops
  61.  
  62.         } catch (IOException ioe) {
  63.  
  64.             // Woops
  65.  
  66.         }
  67.  
  68.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I apologize if any of that isn't decipherable. That's just copy/pasted from my scratchpad.
ismarc
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 29, 2008 6:09 am

Postby dave » Fri Oct 31, 2008 9:00 pm

Magic ismarc! That helped me out completely! Cheers! :D
dave
Junior Developer
Junior Developer
 
Posts: 11
Joined: Fri Oct 31, 2008 5:29 pm
Location: Kent

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests