Help with login to a website!

  • Replies:8
Oliver Halstead
  • Forum posts: 3

Feb 7, 2013, 9:28:26 PM via Website

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

Reply
Oliver Halstead
  • Forum posts: 3

Feb 19, 2013, 3:48:53 PM via Website

Updated with more info!

Reply
Oliver Halstead
  • Forum posts: 3

Feb 21, 2013, 11:22:16 AM via Website

Solved the problem, I just had to find out all the tokens and variables being passed! PM me if anyone needs help with this.

Reply
9android
  • Forum posts: 3

Mar 8, 2013, 4:38:07 AM via Website

i think you may find some useful info at 9android.net/android-login-and-registration-with-php-mysql-and-sqlite/.

Reply
9android
  • Forum posts: 3

Mar 8, 2013, 4:39:08 AM via Website

you can read at 9android.net/android-login-and-registration-with-php-mysql-and-sqlite/. i thinks it 's useful for you.

Reply
9android
  • Forum posts: 3

Mar 8, 2013, 4:43:25 AM via Website

you can read at 9android.net for more.

Reply
Kushal Arora
  • Forum posts: 1

May 10, 2014, 10:15:42 AM via Website

How to initiate, this whole scenario
m quite new, have made some basic apps working great

But what aftr setting up layout for login??
two textboxes a button! layout setup done
now what??

can I have the procedure, of what all is required to fetch the data from a website(after log into it)
all technologies, httpclient,JSON, SOUP whatever.
I searched a lot and kinda got confused

Info: Website is being built on .NET platform , Server used: Microsoft-IIS/6 server
any help would be appreciated
thank you

— modified on May 10, 2014, 10:18:13 AM

Reply
do thong thai
  • Forum posts: 12

May 14, 2014, 6:05:49 AM via Website

it's seem hard ^_^

Reply
Madhumitha N
  • Forum posts: 1

Aug 23, 2015, 5:39:43 PM via Website

Hi,
What do you mean by tokens and variables? Please clarify. I'm having the same problem and I can't login to a website!. Thank you.

Reply