/**
* Project: pts.biz
*
* File Created at 2012-9-11
* $Id$
*
* Copyright 1999-2100 Alibaba.com Corporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Alibaba Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Alibaba.com.
*/
package com.alibaba.pts.report;
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* TODO Comment of SimpleHttpClient
* @author wb_jing.xiongj
*
*/
public class SimpleHttpClient {
static final String LOGON_SITE = "pts.data.alibaba-inc.com" ;
static final int LOGON_PORT = 80;
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( LOGON_SITE , LOGON_PORT, "http" );
// client.getParams().setParameter("http.protocol.single-cookie-header", true);
HttpMethod method1 = loginMethod();
//重定向处理
// if(method1.getStatusCode()==302){
// // 读取新的 URL 地址
// Header header=method1.getResponseHeader("location");
// if (header!=null){
// String newuri=header.getValue();
// if((newuri==null)||(newuri.equals(""))){
// newuri="/";
// }
// System.out.println(newuri);
// GetMethod redirect=new GetMethod(newuri);
// client.executeMethod(redirect);
// System.out.println("Redirect:"+redirect.getStatusLine().toString());
// redirect.releaseConnection();
// }else
// System.out.println("Invalid redirect");
// }
// 查看 cookie 信息
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.executeMethod(method1); //模拟登陆
method1.releaseConnection();
System.out.println(method1.getStatusLine());
Cookie[] cookies = client.getState().getCookies();
if (cookies.length == 0) {
System.out.println( "None" );
} else {
for ( int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
}
}
HttpMethod method2 = getPostMethod();
client.executeMethod(method2);
System.out.println(method2.getStatusLine());
System.out.println(method2.getStatusCode());
String response=new String(method2.getResponseBodyAsString().getBytes("utf-8"));
System.out.println(response);
method2.releaseConnection();
}
public static PostMethod loginMethod(){
PostMethod post = new PostMethod( "/login.dox" );
NameValuePair value1 = new NameValuePair( "loginId" , "icbu-admin" );
NameValuePair value2 = new NameValuePair( "pwd" , "123456" );
NameValuePair value3 = new NameValuePair( "cookieTimeout" , "1" );
NameValuePair value4 = new NameValuePair( "action" , "login/LoginAction" );
NameValuePair value5 = new NameValuePair( "event_submit_do_login" , "true" );
post.setRequestBody( new NameValuePair[] { value1,value2,value3,value4,value5});
return post;
}
public static PostMethod getPostMethod(){
PostMethod post = new PostMethod( "/offlinereport.dox" );
NameValuePair value1 = new NameValuePair( "action" , "report/OffLineReportAction" );
NameValuePair value2 = new NameValuePair( "event_submit_do_query_off_line_report" , "true" );
NameValuePair value3 = new NameValuePair( "sort" , "asc" );
NameValuePair value4 = new NameValuePair( "sortCol" , "id" );
NameValuePair value5 = new NameValuePair( "reportLevel" , "1" );
NameValuePair value6 = new NameValuePair( "advertiserId" , "2" );
NameValuePair value7 = new NameValuePair( "page" , "1" );
NameValuePair value8 = new NameValuePair( "pageSize" , "10" );
post.setRequestBody( new NameValuePair[] { value1,value2,value3,value4,value5,value6,value7,value8});
return post;
}
}