我正在制作一个基本代理,我正在尝试向客户端发送响应并缓存此响应以供将来使用。
我有以下功能:
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;
}
如果我使用inconn.sendResponseEntity(response);
,客户端会毫无困难地获得响应。
但如果我写:
String sourceString = EntityUtils.toString(response.getEntity());
inconn.sendResponseHeader(response);
inconn.sendResponseEntity(response);
该页面未提供给客户端,因为HttpEntity已被使用。
关于如何解决这个问题的任何想法?
答案 0 :(得分:1)
每个实体类都声明其是否可重复....如上面的代码注释中所示。验证您用于响应的实体类以及实体是否可重复。如果是,那么我认为你可以再次为你的缓存调用'getEntity()'。
答案 1 :(得分:0)
来自开放连接的流支持的HTTP实体本质上是不可重复的,因此不能多次使用。您唯一的选择是通过在内存或文件中缓冲其内容来使实体可重复。