Web authentication Confirmation : Authentication « Security « Android






Web authentication Confirmation

    
//package com.maxiujun.android.doudroid.test.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
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 org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

/**
 * Type comments here.
 * 
 * @author Xiujun Ma <maxj@adv.emcom.jp>
 * @version Jul 31, 2010
 */
class OauthWebConfirm {
  private static HttpClient httpClient = new CHttpClient();
  public static String email = "";
  public static String pwd = "";

  public static void confirm(String url) {
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    try {

      // login
      HttpPost loginpost = new HttpPost("http://www.douban.com/login");

      String loginentity = "redir=&form_email=" + URLEncoder.encode(email, "UTF-8") + "&form_password=" + URLEncoder.encode(pwd, "UTF-8") +"&remember=on&user_login=%E8%BF%9B%E5%85%A5";

      StringEntity reqEntity = new StringEntity(loginentity);
      reqEntity.setContentType("application/x-www-form-urlencoded");
      loginpost.setEntity(reqEntity);
      loginpost.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
      httpClient.execute(loginpost);
      
      // agree page
      HttpGet get = new HttpGet(url);
      get.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
      HttpResponse res2 = httpClient.execute(get);
      String restring2 = convertStreamToString(res2.getEntity().getContent());

      // agree action
      HttpPost agreepost = new HttpPost(url);
      
      StringBuilder stringBuilder = new StringBuilder();

      stringBuilder.append("oauth_token=").append(getFromValue(restring2, "oauth_token"));
      stringBuilder.append("&oauth_callback=").append(getFromValue(restring2, "oauth_callback"));
      stringBuilder.append("&ssid=").append(getFromValue(restring2, "ssid"));
      stringBuilder.append("&confirm=").append(getFromValue(restring2, "confirm"));

      StringEntity agreeEntity = new StringEntity(stringBuilder.toString());
      agreeEntity.setContentType("application/x-www-form-urlencoded");
      agreepost.setEntity(agreeEntity);
      agreepost.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
      httpClient.execute(agreepost);
      // HttpResponse res3 = httpClient.execute(agreepost);
      
      // String restring3 = convertStreamToString(res3.getEntity().getContent());
      
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static String convertStreamToString(InputStream is) throws IOException {
    if (is != null) {
      StringBuilder sb = new StringBuilder();
      String line;

      try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        while ((line = reader.readLine()) != null) {
          sb.append(line).append("\n");
        }
      } finally {
        is.close();
      }
      return sb.toString();
    } else {
      return "";
    }
  }
  
  public static String getFromValue(String html, String name) {
    
    Pattern p = Pattern.compile("name=\\\"" + name + "\\\" value=\\\".+\\\"");
    Matcher m = p.matcher(html);
    String ex = "";
    if(m.find()) ex = m.group(0);
    else return "";
    String value = "";
        try {
          value = URLEncoder.encode(ex.split("value=\"")[1].replace("\"", ""), "UTF-8");
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
    return value;
  }
}



class CHttpClient extends DefaultHttpClient {
  @Override
  protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    
    return new ThreadSafeClientConnManager(this.getParams(), registry);
  }
}

   
    
    
    
  








Related examples in the same category

1.Provides symmetric key cryptography and hashing.
2.Returns permissions' name (human-readable label) by permission key