Moms4j.java :  » Client » com-android-moms-client » com » moms » test » twitter » library » Android Open Source

Android Open Source » Client » com android moms client 
com android moms client » com » moms » test » twitter » library » Moms4j.java
package com.moms.test.twitter.library;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Moms4j {
    static final String LOGON_SITE = "www.moms.kr";
    static final int    LOGON_PORT = 80;
  final String momsLogin = "/index_open.html";     //    
  public String username;
  public String password;
  // save internal state 0: not loged-in, 1: loged-in
  public int curStatus = 0;
  
  private HttpClient client = new HttpClient();
  // Get initial state object
    HttpState initialState = new HttpState();

    
  public int Login(String userid, String password) throws HttpException, IOException
  {
    
    this.username = userid;
    this.password = password;
    

        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    
    GetMethod getMethod = new GetMethod(momsLogin);

    // Get the first page to get cookies.
    int statusCode = 0;
    try {
      statusCode = client.executeMethod(getMethod);
    } catch (HttpException e1) {
      e1.printStackTrace();
      return -1;
    } catch (IOException e1) {
      e1.printStackTrace();
    } 
    
    if (statusCode != HttpStatus.SC_OK) {
      System.err.println("Method failed: " + getMethod.getStatusLine());
    } 

        System.out.println("Login form get: " + getMethod.getStatusLine().toString()); 
        // release any connection resources used by the method
        getMethod.releaseConnection();
        // See if we got any cookies
        CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
        Cookie[] initcookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
        System.out.println("Initial set of cookies:");    
        if (initcookies.length == 0) {
          System.out.println("None");    
        } 
        else 
        {
          for (int i = 0; i < initcookies.length; i++) {
            System.out.println("- " + initcookies[i].toString());    
          }
        }
     
        PostMethod authpost = new PostMethod("/login/login.php");
        // Prepare login parameters
        NameValuePair id   = new NameValuePair("id", this.username);
        NameValuePair pw      = new NameValuePair("password", this.password);
        NameValuePair security   = new NameValuePair("security", "1");
        NameValuePair auto_id = new NameValuePair("auto_id", "1");
        authpost.setRequestBody(new NameValuePair[] {id, pw, security, auto_id});
        client.executeMethod(authpost);
        System.out.println("Login form post: " + authpost.getStatusLine().toString()); 
        // release any connection resources used by the method
        authpost.releaseConnection();
        // See if we got any cookies
        // The only way of telling whether logon succeeded is 
        // by finding a session cookie
        Cookie[] logoncookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
        System.out.println("Logon cookies:");    
        if (logoncookies.length == 0) {
          System.out.println("None");    
        } 
        else 
        {
          for (int i = 0; i < logoncookies.length; i++) {
            System.out.println("- " + logoncookies[i].toString());    
          }
        }
        // Usually a successful form-based login results in a redicrect to 
        // another url
        int statuscode = authpost.getStatusCode();
        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
          (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) 
        {
          Header header = authpost.getResponseHeader("location");
          if (header != null) {
            String newuri = header.getValue();
            if ((newuri == null) || (newuri.equals(""))) {
              newuri = "/";
            }
            System.out.println("Redirect target: " + newuri); 
            GetMethod redirect = new GetMethod(newuri);
            client.executeMethod(redirect);
            System.out.println("Redirect: " + redirect.getStatusLine().toString()); 
/*
            InputStream responseBody = null;
          //print the result
          responseBody = redirect.getResponseBodyAsStream();
          byte[] buf = new byte[1024];
          while(true){
               int data = responseBody.read(buf); 
               p(new String(buf));
               if(data == -1){
                 System.out.println("file end");
                 break;
               }
          }
*/
            redirect.releaseConnection();
          } 
          else 
          {
            System.out.println("Invalid redirect");
            System.exit(1);
        }
        }
        
        
    return 1;
  }
  
  public String getList(String userid) throws HttpException, IOException
  { 
    //    POST /talk/act.php HTTP/1.1i
    String uri = "http://api.moms.kr/TalkMsgList/talkmsg_recent_json.html";
    String option1 = "?type=list&kind=mytalk&tb=org&";
    String option2 = "tl_id="+userid+"&view_id="+userid;
    String option3 = "&no=&cnt=20&sdate=&";
    
    GetMethod getMethod = new GetMethod(uri+option1+option2+option3); 
    client.executeMethod(getMethod);
    InputStream responseBody = null;
    //print the result
    responseBody = getMethod.getResponseBodyAsStream();
    byte[] buf = new byte[1024];
    String ret="";
    while(true){
         int data = responseBody.read(buf); 
         ret += new String(buf);
         if(data == -1){
           break;
         }
    }
    return ret;
  }
    private static void p(int s) {
        System.out.println(s);
    }
    private static void p(String s) {
        System.out.println(s);
    }
}
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.