httpclient例子

package com.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class Dbsight {

//登录的地址
static final String LOGIN_ADDR = "http://192.168.0.240:8080/dbsight/login.do";
//scheduleAJob.do的地址
static final String COMMIT_ADDR = "http://192.168.0.240:8080/dbsight/scheduleAJob.do?indexName=develop_test&cmd=stopIndexing%20updateIndex%20mergeTempIndexes%20updateTempIndex%20mergeIndexesIfNeeded%20updateIndex%20unlockStoppedIndex%20pruneBeforeIndexing%20updateIndex%20createPeriodTable%20unlockStoppedIndex%20maybeBuildSynonyms%20incrementalIndexing%20mergeTempIndexes%20updateTempIndex%20mergeIndexesIfNeeded%20updateIndex%20buildDictionaryIfNeeded%20ping-a-url&text=Incremental%20Indexing";
//最后执行页的地址
static final String LOG_ADDR = "http://192.168.0.240:8080/dbsight/showIndexLog.do?indexName=develop_test";
//登录密码
static final String LOGIN_PASSWORD = "chinese";
//是否记住密码
static final String LOGIN_REMEBERME = "true";
private HttpClient client = new HttpClient();

public Dbsight(){

//登录的参数
NameValuePair[] loginPair = {
new NameValuePair("operation", "login"),
new NameValuePair("password", LOGIN_PASSWORD),
new NameValuePair("remeberme", LOGIN_REMEBERME)};
//登录method
PostMethod loginMethod = this.getPostMethod(LOGIN_ADDR, loginPair);
loginMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());

try {
client.executeMethod(loginMethod);
//执行状态
int loginStatusCode = loginMethod.getStatusCode();
//System.out.println(" loginbody :"+loginMethod.getResponseBodyAsString());
//登录方法结束
loginMethod.releaseConnection();
//跳转执行
if (loginStatusCode == HttpStatus.SC_MOVED_PERMANENTLY || loginStatusCode == HttpStatus.SC_MOVED_TEMPORARILY || loginStatusCode == HttpStatus.SC_SEE_OTHER || loginStatusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
Header loginLocationHeader = loginMethod.getResponseHeader("location");
String location = null;
if(loginLocationHeader != null){
location = loginLocationHeader.getValue();

}
//登录成功
}else if(loginStatusCode == 200){
//创建scheduleAJob.do的method
GetMethod commitMethod = this.getGetMethod(COMMIT_ADDR);

commitMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
client.executeMethod(commitMethod);
//执行结果
int commitStatusCode = loginMethod.getStatusCode();
//System.out.println(" commitbody: "+ commitMethod.getResponseBodyAsString());
commitMethod.releaseConnection();
System.out.println(" commitStatusCode is : "+ commitStatusCode);
//执行成功
if(commitStatusCode == 200){
//执行页MOTHOD
GetMethod logMethod = this.getGetMethod(LOG_ADDR);
logMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
//执行
client.executeMethod(logMethod);
//执行结果代码
int logStatusCode = logMethod.getStatusCode();
System.out.println(" logStatusCode is : "+ logStatusCode);
System.out.println(" logbody is : "+ logMethod.getResponseBodyAsString());
logMethod.releaseConnection();

}

}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

public HttpClient getClient() {
return client;
}

public void setClient(HttpClient client) {
this.client = client;
}



/**
* 使用 GET 方式提交数据
*@return
*/


private GetMethod getGetMethod(String URL){
return new GetMethod(URL);
}

/**
* 使用 POST 方式提交数据
*@return
*/


private PostMethod getPostMethod(String URL,NameValuePair[] pair ){
PostMethod post = new PostMethod(URL );
post.setRequestBody( pair);
return post;
}

public static void main(String args[]){
new Dbsight();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值