LoginPageHandler.java :  » Browser » mde-android » de » kamelstall » modsde » comm » Android Open Source

Android Open Source » Browser » mde android 
mde android » de » kamelstall » modsde » comm » LoginPageHandler.java
package de.kamelstall.modsde.comm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;

import android.util.Log;

/**
 * This class does nothing more, than waiting for a response from a request to
 * the misc.php. Once received the source code is parsed and the
 * mods.de/setcookie.php file is called.
 */
public class LoginPageHandler implements ResponseListener {
  
  public static String url(String username, String password) throws UnsupportedEncodingException {
    return MdeStatus.URL_LOGIN
        + "&login_username=" + URLEncoder.encode(username, MdeStatus.DEFAULT_ENCODING)
        + "&login_password=" + URLEncoder.encode(password, MdeStatus.DEFAULT_ENCODING);
  }
  
  @Override
  public void onResponseReceived(HttpResponse response) {
    try {
      try {
        String encoding = MdeStatus.DEFAULT_ENCODING;
        if (response.getEntity().getContentEncoding() != null) {
          encoding = response.getEntity().getContentEncoding().getValue();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), encoding));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
          sb.append(line).append("\n");
        }

        String input = sb.toString();
        // search for setcookie.php
        if (input.contains("setcookie.php")) {
          Pattern pattern = Pattern.compile("http://mods.de/setcookie[^\"]*");
          Matcher m = pattern.matcher(input);
          if (m.find()) {
            Log.d("MDE", "Found: " + m.group());
            // send request to set cookies.
            HTTPClient.sendRequest(m.group(), null);
          }
        }
        else {
          // "Wrong login :("
        }
        
      } finally {
        // indicate, that content has been consumed
        response.getEntity().consumeContent();
      }
    } catch (IOException e) {

    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.