最近搞一个扣网页内容的SessionBean,需要模拟客户端post提交,然后得到servlet返回的结果。
采用Jakarta的HttpClient API解决之.
HttpClient扩展和增强了标准java.net包,是一个内容广泛的代码库,功能极其丰富,能够构造出各
种使用HTTP协议的分布式应用,或者也可以嵌入到现有应用,为应用增加访问HTTP协议的能力
要求:
1:CLASSPATH中有commons-httpclient.jar,common-logging.jar
2:确保%JAVA_HOME% /jre/lib/security/java.security文件包含这行代码:
security.provider.2= com.sun.net.ssl.internal.ssl.Provider。
一:GET方法测试代码:
/**/
/*
*CreatedonSep25,2006
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
package
co.iproxy.http;

import
java.io.IOException;

import
org.apache.commons.httpclient.HttpClient;
import
org.apache.commons.httpclient.HttpException;
import
org.apache.commons.httpclient.HttpStatus;
import
org.apache.commons.httpclient.methods.GetMethod;


/***/
/**
*@authorlichunlei
*
*Tochangethetemplateforthisgeneratedtypecommentgoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
public
class
HttpClientGetMethodTest

...
{

publicstaticvoidmain(String[]args)

...{
HttpClientclient=newHttpClient();
Stringurl=http://192.18.0.19:9080/care/careTest/index.jsp;
GetMethodmethod=newGetMethod(url);

try

...{
client.executeMethod(method);
if(method.getStatusCode()==HttpStatus.SC_OK)

...{
Stringresponse=method.getResponseBodyAsString();
System.out.println(response);
}

}
catch(HttpExceptione)

...{
e.printStackTrace();

}
catch(IOExceptione)

...{
e.printStackTrace();
}
finally

...{

method.releaseConnection();
method.recycle();
}

}
}
二:POST方法测试代码:
/**/
/*
*CreatedonSep25,2006
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
package
co.iproxy.http;

import
java.io.BufferedReader;
import
java.io.ByteArrayInputStream;
import
java.io.File;
import
java.io.FileNotFoundException;
import
java.io.FileReader;
import
java.io.IOException;
import
java.io.InputStream;

import
org.apache.commons.httpclient.DefaultMethodRetryHandler;
import
org.apache.commons.httpclient.HostConfiguration;
import
org.apache.commons.httpclient.HttpClient;
import
org.apache.commons.httpclient.HttpStatus;
import
org.apache.commons.httpclient.methods.PostMethod;


/***/
/**
*@authorlichunlei
*
*Tochangethetemplateforthisgeneratedtypecommentgoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
public
class
HttpClientPostMethodTest

...
{

staticintBASE_BODY_SIZE=10240;
staticintINC_BODY_SIZE=51200;

publicstaticvoidmain(String[]args)

...{

Stringrequest=null;
Stringurl=http://12.20.69.50:5080/Web/Servicelet;
Stringresult=null;

StringfilePath="D:/OPS_piese_idl36(fromcvs)/Ntelagent/co/iproxy/http/request.txt";
Filef=newFile(filePath);
FileReaderfileReader=null;
try

...{
fileReader=newFileReader(f);
BufferedReaderbufferedReader=newBufferedReader(fileReader);
StringcurrentLine;
StringBuffercontent=newStringBuffer();
while((currentLine=bufferedReader.readLine())!=null)

...{
content.append(currentLine);
}
request=content.toString();
}
catch(Exceptione1)

...{
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}

System.out.println("therequestis:"+request);

DefaultMethodRetryHandlerretryhandler=newDefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(true);
retryhandler.setRetryCount(2);//retry2times

HttpClienthttpClient=newHttpClient();
PostMethodmethod=newPostMethod(url);

InputStreamdata=newByteArrayInputStream(request.getBytes());
method.setRequestBody(data);

method.setFollowRedirects(true);
method.setMethodRetryHandler(retryhandler);

try

...{

//executethemethod
HostConfigurationcf=newHostConfiguration();
System.out.println("useproxy");
cf.setProxy("192.18.254.22",4480);
httpClient.setHostConfiguration(cf);
//httpClient.setTimeout(10000000);

intretcode=httpClient.executeMethod(method);

if(retcode==HttpStatus.SC_OK)

...{
byte[]responseBody=newbyte[BASE_BODY_SIZE];
java.io.InputStreamistream=method.getResponseBodyAsStream();
intnpos=0;
intnread=0;
while((nread=istream.read(responseBody,npos,responseBody.length-npos))>=0)

...{
npos+=nread;
if(npos>=responseBody.length)

...{
byte[]tmpBuf=newbyte[npos+INC_BODY_SIZE];
System.arraycopy(responseBody,0,tmpBuf,0,npos);
responseBody=tmpBuf;
}
}

result=newString(responseBody,0,npos);
}
else

...{
thrownewIOException("failedtosendrequest:retcode:"+retcode);
}

}
catch(Exceptione)

...{
}
finally

...{
System.out.println("lcltestinhttpClient:"+result);

}

}
}

以上两个class已经包含了大部分常用的模拟http客户端的技术了,包括设置代理服务器,提交表单,得到返回结果等.
通过上面的测试代码,已经可以初步解决扣网页的问题了.现在备份一下我的实现方法(不是很通用,需要进一步完善), 同时也供大家参考.
/**/
/*
*CreatedonSep25,2006
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
package
co.iproxy.http;

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

import
java.net.MalformedURLException;

import
org.apache.commons.httpclient.DefaultMethodRetryHandler;
import
org.apache.commons.httpclient.HostConfiguration;
import
org.apache.commons.httpclient.HttpClient;
import
org.apache.commons.httpclient.HttpStatus;
import
org.apache.commons.httpclient.methods.PostMethod;


/***/
/**
*@authorlichunlei
*
*Tochangethetemplateforthisgeneratedtypecommentgoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/

public
class
HttpService
implements
ServiceInterface
...
{

privateHttpClienthttpClient=newHttpClient();

privateDefaultMethodRetryHandlerretryhandler=null;

privateStringurl=null;

staticintBASE_BODY_SIZE=10240;
staticintINC_BODY_SIZE=51200;


publicHttpService()throwsMalformedURLException...{
this.createMethodRetryHandler();
this.setHostConfiguration();
}


privatevoidcreateMethodRetryHandler()...{
retryhandler=newDefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(true);
retryhandler.setRetryCount(2);//retry2times
}


privatevoidsetHostConfiguration()throwsMalformedURLException...{
this.url=http://12.20.69.80:4080/Web/Servicelet;
Stringhost="12.21.6.81";
intport=5080;

Stringprotocol="http";


if(url!=null&&