HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
httpexecutor.setParams(params);
// Required protocol interceptors
httpexecutor.addInterceptor(new RequestContent());
httpexecutor.addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
httpexecutor.addInterceptor(new RequestConnControl());
httpexecutor.addInterceptor(new RequestUserAgent());
httpexecutor.addInterceptor(new RequestExpectContinue());
HttpHost host = new HttpHost("localhost", 8080);
HttpClientConnection conn = new DefaultHttpClientConnection(host);
try {
HttpEntity[] requestBodies = {
new StringEntity(
"This is the first test request", "UTF-8"),
new ByteArrayEntity(
"This is the second test request".getBytes("UTF-8")),
new InputStreamEntity(
new ByteArrayInputStream(
"This is the third test request (will be chunked)"
.getBytes("UTF-8")), -1)
};
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
for (int i = 0; i < requestBodies.length; i++) {
HttpPost request = new HttpPost("/servlets-examples/servlet/RequestInfoExample");
request.setEntity(requestBodies[i]);
System.out.println(">> Request URI: " + request.getRequestLine().getUri());
HttpResponse response = httpexecutor.execute(request, conn);
System.out.println("<< Response: " + response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
if (!connStrategy.keepAlive(response)) {
conn.close();