I'm making a basic proxy, and I'm currently trying to send a response to the client AND to cache this response for future use.
I have the following function:
public HttpResponse sendRequestToServer(HttpRequest request)
{
int bufsize = 8 * 1024;
HttpHost host = this.getHost(request);
HttpResponse response = null;
try
{
Socket outsocket = new Socket(host.getHostName(), host.getPort());
DefaultBHttpClientConnection outconn = new DefaultBHttpClientConnection(bufsize);
outconn.bind(outsocket);
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new RequestContent())
.add(new RequestTargetHost())
.add(new RequestConnControl())
.add(new RequestUserAgent("ProxyServer 1.0"))
.add(new RequestExpectContinue(true)).build();
System.out.println("Outgoing connection to : " + outsocket.getInetAddress());
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpCoreContext context = HttpCoreContext.create();
context.setTargetHost(host);
httpexecutor.preProcess(request, httpproc, context);
response = httpexecutor.execute(request, outconn, context);
httpexecutor.postProcess(response, httpproc, context);
}
catch(IOException | HttpException e)
{
System.err.println("Error sending request: " + e);
}
return response;
}
If I use inconn.sendResponseEntity(response);
, the client get the response without any trouble.
Yet if I write:
String sourceString = EntityUtils.toString(response.getEntity());
inconn.sendResponseHeader(response);
inconn.sendResponseEntity(response);
The page is not served to the client because the HttpEntity has already been consumed.
Any idea on how to solve this?