最近搞一个扣网页内容的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方法测试代码:
/**/
/*
* Created on Sep 25, 2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
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;


/** */
/**
* @author lichunlei
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public
class
HttpClientGetMethodTest

...
{

public static void main(String[] args)

...{
HttpClient client = new HttpClient();
String url = http://192.18.0.79:9080/Icare/IcareTest/index.jsp;
GetMethod method = new GetMethod(url);

try

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

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

}
catch (HttpException e)

...{
e.printStackTrace();

}
catch (IOException e)

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

...{

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

}
}
二:POST方法测试代码:
/**/
/*
* Created on Sep 25, 2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
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;


/** */
/**
* @author lichunlei
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public
class
HttpClientPostMethodTest

...
{

static int BASE_BODY_SIZE = 10240;
static int INC_BODY_SIZE = 51200;

public static void main(String[] args)

...{

String request = null;
String url = http://13.201.109.70:8080/Web/Servicelet;
String result = null;

String filePath = "D:/OPS_piese_idl36(from cvs)/Ntelagent/co/iproxy/http/request.txt";
File f = new File(filePath);
FileReader fileReader = null;
try

...{
fileReader = new FileReader(f);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String currentLine;
StringBuffer content = new StringBuffer();
while ((currentLine = bufferedReader.readLine()) != null)

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

...{
// TODO Auto-generated catch block
e1.printStackTrace();
}

System.out.println("the request is: " + request);

DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
retryhandler.setRequestSentRetryEnabled(true);
retryhandler.setRetryCount(2); // retry 2 times

HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(url);

InputStream data = new ByteArrayInputStream(request.getBytes());
method.setRequestBody(data);

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

try

...{

// execute the method
HostConfiguration cf = new HostConfiguration();
System.out.println("use proxy");
cf.setProxy("192.168.24.22", 4480);
httpClient.setHostConfiguration(cf);
//httpClient.setTimeout(10000000);

int retcode = httpClient.executeMethod(method);

if (retcode == HttpStatus.SC_OK)

...{
byte[] responseBody = new byte[BASE_BODY_SIZE];
java.io.InputStream istream = method.getResponseBodyAsStream();
int npos = 0;
int nread = 0;
while ((nread = istream.read(responseBody, npos, responseBody.length - npos)) >= 0)

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

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

result = new String(responseBody, 0, npos);
}
else

...{
throw new IOException("failed to send request: retcode: " + retcode);
}

}
catch (Exception e)

...{
}
finally

...{
System.out.println("lcl test in httpClient:" + result);

}

}
}
