我在使用apache的HttpCore实现代理的过程中。 这是我的HttpRequestHandler它基于反向代理示例来自apache link:
static class ProxyHandler implements HttpRequestHandler {
private final HttpHost target;
private final HttpProcessor httpproc;
private final HttpRequestExecutor httpexecutor;
private final ConnectionReuseStrategy connStrategy;
private SocketFactory sf;
public ProxyHandler(
final HttpHost target,
final HttpProcessor httpproc,
final HttpRequestExecutor httpexecutor,
SocketFactory sf) {
super();
this.target = target;
this.httpproc = httpproc;
this.httpexecutor = httpexecutor;
this.sf = sf;
this.connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
}
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final int bufsize = 8 * 1024;
URI uri = null;
try {
uri = new URI(request.getRequestLine().getUri());
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
String domain = uri.getHost();
HttpHost host = new HttpHost(domain, 80);
System.out.println("host "+domain);
DefaultBHttpClientConnection outconn = (DefaultBHttpClientConnection) context.getAttribute(HTTP_OUT_CONN);
final Socket outsocket = sf.createSocket(host.getHostName(), this.target.getPort());
outconn = new DefaultBHttpClientConnection(bufsize);
outconn.bind(outsocket);
System.out.println("My Outgoing connection to " + outsocket.getInetAddress());
context.setAttribute(HTTP_OUT_CONN, outconn);
final HttpClientConnection conn = (HttpClientConnection) context.getAttribute(
HTTP_OUT_CONN);
context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
System.out.println(">> Request URI: " + request.getRequestLine().getUri());
// Remove hop-by-hop headers
request.removeHeaders(HTTP.CONTENT_LEN);
request.removeHeaders(HTTP.TRANSFER_ENCODING);
request.removeHeaders(HTTP.CONN_DIRECTIVE);
request.removeHeaders("Keep-Alive");
request.removeHeaders("Proxy-Authenticate");
request.removeHeaders("TE");
request.removeHeaders("Trailers");
request.removeHeaders("Upgrade");
this.httpexecutor.preProcess(request, this.httpproc, context);
final HttpResponse targetResponse = this.httpexecutor.execute(request, conn, context);
this.httpexecutor.postProcess(response, this.httpproc, context);
// Remove hop-by-hop headers
targetResponse.removeHeaders(HTTP.CONTENT_LEN);
targetResponse.removeHeaders(HTTP.TRANSFER_ENCODING);
targetResponse.removeHeaders(HTTP.CONN_DIRECTIVE);
targetResponse.removeHeaders("Keep-Alive");
targetResponse.removeHeaders("TE");
targetResponse.removeHeaders("Trailers");
targetResponse.removeHeaders("Upgrade");
response.setStatusLine(targetResponse.getStatusLine());
response.setHeaders(targetResponse.getAllHeaders());
response.setEntity(targetResponse.getEntity());
System.out.println("<< Response: " + response.getStatusLine());
final boolean keepalive = this.connStrategy.keepAlive(response, context);
context.setAttribute(HTTP_CONN_KEEPALIVE, new Boolean(keepalive));
outsocket.close();
}
}
我更改了处理程序功能,并在其中创建,
DefaultBHttpClientConnection outconn 基于请求URI中的主机
正如您所看到的,我使用 SocketFactory 创建了一个新套接字( outsocket )。
问题我不知道如何关闭插座。
我尝试在 handle()结束时关闭它,但后来我会开始接收
I / O错误:套接字已关闭
代理停止工作 调用 httpservice.handleRequest()的线程与链接中的示例中的线程相同(如果需要,我可以发布它,我没有导致它在问题中占用空间)。
答案 0 :(得分:1)
我已经解决了这个问题。 这是一行:
outconn = new DefaultBHttpClientConnection(bufsize);
处理程序内部。
所以每次调用处理程序时都会创建一个新的outconn。 以及在调用之前设置的那个:
httpservice.handleRequest(this.inconn, context);
即使它被关闭,它也不会关闭套接字,因为它被绑定到outconn的另一个副本。
答案 1 :(得分:0)
如果您正在创建代理,那么您应该关闭连接的唯一时间是,如果您在一方收到IOException,那将关闭该对话的一半;那时,关闭对话的另一面是安全的