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();
}
}
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();
}
}