httpclient

 

/**

 * 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;       

    }

 

}

 

运行结果:
HTTP/1.1 200 OK
PTS_1_2_3_4_5_6_0000=e388f02f750e65ebba95ab9493cda01e
PTS_1_2_3_4_5_6_0001=ICBU-Admin
PTS_1_2_3_4_5_6_0004=10000
PTS_1_2_3_4_5_6_0002=505744924b61118baff5a85a07ecf287
PTS_1_2_3_4_5_6_0005=2
PTS_1_2_3_4_5_6_0003=ICBU-Admin%7Chua.muh%40alibaba-inc.com
HTTP/1.1 200 OK
200
{"returnValue":{"total":2,"result":[{"id":"4a64c1f3a53544c081a261739c9dffd6","status":1,"startDate":"20120903","errorMessage":"","advertiserId":2,"reportLevel":1,"accountRange":"8417680229,9206984576","userId":"10000","reportType":1,"endDate":"20120909","reportName":"111","distinguishMatchType":0,"reportPath":"C:\\Documents and Settings\\wb_jing.xiongj\\download\\OffLineReport\\20120911\\1000020120911094923_111_1.csv","createDate":"2012-09-11 09:49:23","statusDesc":"成功"},{"id":"8b032d08cc004023ac1a0ffae5bbfa3b","status":1,"startDate":"20120903","errorMessage":"","advertiserId":2,"reportLevel":1,"accountRange":"3028038474,8417680229","userId":"10000","reportType":1,"endDate":"20120909","reportName":"111","distinguishMatchType":0,"reportPath":"C:\\Documents and Settings\\wb_jing.xiongj\\download\\OffLineReport\\20120911\\1000020120911094909_111_1.csv","createDate":"2012-09-11 09:49:09","statusDesc":"成功"}],"page":1},"returnErrorStackTrace":"","successed":true,"returnUrl":"","returnMessage":"","returnCode":0}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值