Help with login to a website!

  • Replies:6
  • OpenNot stickiedUnanswered

Feb 7, 2013 9:28:26 PM

Hi,

I am a complete beginner at this HTTP stuff so bear with me! I have looked around for tutorials and example code to try and help me log in to an online banking system. Here is my code. I believe I may have to add some auth code in the header or some more parameters. Any help would be much appreciated!

1package com.ollyhal.budgetProgram;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.io.UnsupportedEncodingException;
8import java.net.URI;
9import java.util.ArrayList;
10import java.util.List;
11
12import org.apache.http.HttpEntity;
13import org.apache.http.HttpHost;
14import org.apache.http.HttpResponse;
15import org.apache.http.HttpVersion;
16import org.apache.http.NameValuePair;
17import org.apache.http.auth.AuthScope;
18import org.apache.http.auth.UsernamePasswordCredentials;
19import org.apache.http.client.ClientProtocolException;
20import org.apache.http.client.CookieStore;
21import org.apache.http.client.CredentialsProvider;
22import org.apache.http.client.HttpClient;
23import org.apache.http.client.entity.UrlEncodedFormEntity;
24import org.apache.http.client.methods.HttpGet;
25import org.apache.http.client.methods.HttpPost;
26import org.apache.http.client.methods.HttpPut;
27import org.apache.http.conn.scheme.PlainSocketFactory;
28import org.apache.http.conn.scheme.Scheme;
29import org.apache.http.conn.scheme.SchemeRegistry;
30import org.apache.http.conn.ssl.SSLSocketFactory;
31import org.apache.http.entity.StringEntity;
32import org.apache.http.impl.client.BasicCredentialsProvider;
33import org.apache.http.impl.client.DefaultHttpClient;
34import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
35import org.apache.http.message.BasicNameValuePair;
36import org.apache.http.params.BasicHttpParams;
37import org.apache.http.params.HttpConnectionParams;
38import org.apache.http.params.HttpParams;
39import org.apache.http.params.HttpProtocolParams;
40import org.apache.http.util.EntityUtils;
41
42import android.app.Dialog;
43import android.content.Context;
44import android.os.AsyncTask;
45import android.util.Log;
46import android.widget.Toast;
47
48public class PostData extends AsyncTask<Void, String, Boolean> {
49 Context ourContext;
50
51 public PostData(Context c) {
52 ourContext = c;
53 }
54
55 @Override
56 protected Boolean doInBackground(Void... arg0) {
57
58
59 HttpPost httppost = new HttpPost(
60 "lloyds website");
61 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
62 nameValuePairs.add(new BasicNameValuePair("frmLogin:strCustomerLogin_userID", "<myuser>"));
63 nameValuePairs.add(new BasicNameValuePair("frmLogin:strCustomerLogin_pwd", "<mypass>"));
64 nameValuePairs.add(new BasicNameValuePair("frmLogin:btnLogin1", "Continue"));
65 try {
66 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
67 } catch (UnsupportedEncodingException e) {
68 // TODO Auto-generated catch block
69 e.printStackTrace();
70 }
71
72 try {
73 HttpResponse response = getClient().execute(httppost);
74 try {
75
76
77 if(response.getStatusLine().getStatusCode() == 200) {
78
79 HttpEntity entity = response.getEntity();
80 System.out.println("Success");
81
82
83 } else {
84 System.out.println("ERROR");
85 }
86
87 } catch (Exception e) {
88
89 }
90
91
92 } catch (ClientProtocolException e) {
93 // TODO Auto-generated catch block
94 e.printStackTrace();
95 } catch (IOException e) {
96 // TODO Auto-generated catch block
97 e.printStackTrace();
98 }
99 return true;
100 }
101
102 public DefaultHttpClient getClient() {
103 DefaultHttpClient ret = null;
104
105 // sets up parameters
106 HttpParams params = new BasicHttpParams();
107 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
108 HttpProtocolParams.setContentCharset(params, "utf-8");
109 params.setBooleanParameter("http.protocol.expect-continue", false);
110
111 // registers schemes for both http and https
112 SchemeRegistry registry = new SchemeRegistry();
113 registry.register(new Scheme("http", PlainSocketFactory
114 .getSocketFactory(), 80));
115 final SSLSocketFactory sslSocketFactory = SSLSocketFactory
116 .getSocketFactory();
117 sslSocketFactory
118 .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
119 registry.register(new Scheme("https", sslSocketFactory, 443));
120
121 ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(
122 params, registry);
123 ret = new DefaultHttpClient(manager, params);
124 return ret;
125 }
126
127}

It will always return 200 for me no matter what the username and password is! so I do not think the page is being executed properly!

— modified on Feb 19, 2013 3:49:43 PM

Feb 12, 2013 9:36:21 AM

Do you mean you want to make an app which will allow you to log in to one specific account? Or do you mean an application which would allow you to access multiple accounts?

Well I'm new as well to android. I work with php though and this looks like what you need.

androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/