Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package andy.cokezone;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import org.apache.http.Header;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.cookie.Cookie;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.params.HttpParams;
- import org.apache.http.protocol.HTTP;
- import android.app.Activity;
- import android.app.TabActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TabHost;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.TabHost.TabSpec;
- public class CokeZone extends TabActivity {
- /** Called when the activity is first created. */
- static String thisResponse = "";
- TextView debug;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TabHost mTabHost = getTabHost();
- debug = (TextView)findViewById(R.id.debugText);
- mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
- .setIndicator("Main")
- .setContent(R.id.tabview1));
- mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
- .setIndicator("Debug")
- .setContent(R.id.tabview2));
- mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
- .setIndicator("Xtra")
- .setContent(R.id.points));
- mTabHost.setCurrentTab(0);
- String url = "http://www.cokezone.co.uk/home/index.jsp";
- try {
- formLogin(url);
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- OnClickListener debugCallBack = new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Context context = getApplicationContext();
- int duration = Toast.LENGTH_LONG;
- Toast toast = Toast.makeText(context, thisResponse, duration);
- toast.show();
- }
- };
- public DefaultHttpClient formLogin(String url) throws ClientProtocolException, IOException{
- DefaultHttpClient httpclient = new DefaultHttpClient();
- httpclient.getParams().setParameter(
- "http.useragent",
- "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
- );
- HttpGet httpget = new HttpGet("http://wap.cokezone.co.uk");
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
- System.out.println("Login form get: " + response.getStatusLine());
- if (entity != null) {
- entity.consumeContent();
- }
- System.out.println("Initial set of cookies:");
- List<Cookie> cookies = httpclient.getCookieStore().getCookies();
- if (cookies.isEmpty()) {
- System.out.println("None");
- } else {
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("- " + cookies.get(i).toString());
- }
- }
- String homepage = printPage(entity.getContent());
- Scanner scanner = new Scanner(homepage);
- String temp ;
- String login = null;
- while(scanner.hasNext()){
- temp = scanner.nextLine();
- if(temp.contains("Log")){
- Log.e("debug", "Found:"+temp);
- login = temp.split("\"")[7];
- Log.e("debug","logon form:"+login);
- }
- }
- HttpGet httpgetLogin = new HttpGet(login);
- HttpResponse response2 = httpclient.execute(httpgetLogin);
- HttpEntity entity2 = response2.getEntity();
- String loginPage = printPage(entity2.getContent());
- scanner = new Scanner(loginPage);
- login = null;
- while(scanner.hasNext()){
- temp = scanner.nextLine();
- if(temp.contains("form action")){
- Log.e("debug", "Found:"+temp);
- login = temp.split("\"")[1];
- Log.e("debug","logon post:"+login);
- }
- }
- //Got LOGIN POST to create:
- HttpPost httpost = new HttpPost(login);
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- nvps.add(new BasicNameValuePair("email", "renegadeandy@gmail.com"));
- nvps.add(new BasicNameValuePair("password","qsceszqwe"));
- nvps.add(new BasicNameValuePair("go", ",+Login"));
- httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
- response = httpclient.execute(httpost);
- entity = response.getEntity();
- System.out.println("Login form get: " + response.getStatusLine());
- if (entity != null) {
- thisResponse = printPage(entity.getContent());
- entity.consumeContent();
- }
- Log.e("debug",thisResponse);
- Log.e("debug","done");
- // httpget = new HttpGet("http://www.cokezone.co.uk/home/index.jsp");
- // response = httpclient.execute(httpget);
- // entity = response.getEntity();
- TextView points = (TextView)findViewById(R.id.points);
- points.setText(getPoints(entity.getContent()).toString());
- System.out.println("Post logon cookies:");
- cookies = httpclient.getCookieStore().getCookies();
- if (cookies.isEmpty()) {
- System.out.println("None");
- } else {
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("- " + cookies.get(i).toString());
- }
- }
- //ok so now logged in
- return httpclient;
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- //httpclient.getConnectionManager().shutdown();
- }
- public String getPoints(InputStream s) throws IOException{
- byte[] bytes = new byte[1000];
- StringBuilder x = new StringBuilder();
- int numRead = 0;
- while ((numRead = s.read(bytes)) >= 0) {
- x.append(new String(bytes, 0, numRead));
- }
- String html = x.toString();
- debug.setText(html);
- if(html.contains("Andy")){
- return "YES!";
- }
- else{
- return "NO!";
- }
- }
- public String printPage(InputStream s) throws IOException{
- byte[] bytes = new byte[1000];
- StringBuilder x = new StringBuilder();
- int numRead = 0;
- while ((numRead = s.read(bytes)) >= 0) {
- x.append(new String(bytes, 0, numRead));
- }
- return x.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement