https 拦截成功 怎么不让他发送到 真实的服务器
https 拦截成功 怎么不让他发送到 真实的服务器 就是直接把包链接掉 不向这个被拦截的服务器请求
直接return,不往下调用就行了。 示例:
pipeline.addLast(new HttpProxyIntercept() {
@Override
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest,
HttpProxyInterceptPipeline pipeline) throws Exception {
if (HttpUtil.checkUrl(pipeline.getHttpRequest(), "^www.baidu.com$")) {
//拦截到百度的请求,处理完直接返回响应
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.FOUND);
response.headers().set(HttpHeaderNames.LOCATION, "http://www.taobao.com");
HttpContent content = new DefaultLastHttpContent();
clientChannel.writeAndFlush(response);
clientChannel.writeAndFlush(content);
clientChannel.close();
return;
}
pipeline.beforeRequest(clientChannel, httpRequest);
}
});
@monkeyWie I can intercept the request without sending the original request to server by above code you have provided. But how can I achieve the same function using FullRequestIntercept ? I checked the code and there is no way to call the clientChannel. The code below is final, so I can not overide it: public final void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) if use another : public void beforeRequest(Channel clientChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) it will not hit the call entrance. 我想在**FullRequestIntercept** 中也实现同样的功能,不知如何处理? 这个类里面好像没有clientChannel 作为入参的方法,另外一个方法不会命中。
@T0DDY You can use pipeline arg such as pipeline.getClientChannel().
@T0DDY You can use
pipelinearg such aspipeline.getClientChannel().
@monkeyWie I have reviewed the class HttpProxyInterceptPipeline , but I'm afraid there is no such function like pipeline.getClientChannel()
May I add a function like
public void handleRequest(Channel clientChannel, FullHttpRequest httpRequest, HttpProxyInterceptPipeline pipeline)
in the father class FullRequestIntercept then modify the final beforeRequest with a re-package jar?
@T0DDY You can use
pipelinearg such aspipeline.getClientChannel().@monkeyWie I have reviewed the class
HttpProxyInterceptPipeline, but I'm afraid there is no such function likepipeline.getClientChannel()
![]()
May I add a function like
public void handleRequest(Channel clientChannel, FullHttpRequest httpRequest, HttpProxyInterceptPipeline pipeline)in the father classFullRequestInterceptthen modify the finalbeforeRequestwith a re-package jar?

@T0DDY This my mistake, you can also add a HttpProxyIntercept to interrupt the request, like this:
pipeline.addLast(new HttpProxyIntercept(){
@Override
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
clientChannel.close();
}
});
@T0DDY This my mistake, you can also add a
HttpProxyInterceptto interrupt the request, like this:pipeline.addLast(new HttpProxyIntercept(){ @Override public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception { clientChannel.close(); } });
I have tried it, failed. With my investigation, it may because the beforeRequest() in FullRequestIntercept is declared as Final and if both HttpProxyIntercept and FullRequestIntercept have this method, FullRequestIntercept handleRequest will not be called.
In summary, if I deploy FullRequestIntercept to intercept the request, add another HttpProxyIntercept will disable it. And somehow the pipeline will looks like :
pipeline.addLast(HttpProxyIntercept)
pipeline.addLast(FullRequestIntercept)
pipeline.addLast(FullResponseIntercept)
In my opinion, the most reasonable way is to implement another :
public void handleRequest(Channel clientChannel, FullHttpRequest httpRequest, HttpProxyInterceptPipeline pipeline)
and make it compatible with previous versions in FullRequestIntercept .
What do you think about it?
BTW, I have sanity test the new implementation above, it works fine as modify body/header/response without sending to server.
@monkeyWie
I tested ok, that pipeline like this:
pipeline.addLast(FullRequestIntercept)
pipeline.addLast(HttpProxyIntercept)
pipeline.addLast(FullResponseIntercept)
so can you try again following the above? for example:
pipeline.addLast(new FullRequestIntercept() {
@Override
public boolean match(HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) {
return true;
}
});
pipeline.addLast(new HttpProxyIntercept() {
@Override
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
FullHttpRequest req = (FullHttpRequest) httpRequest;
System.out.println(req.content().toString(Charset.defaultCharset()));
clientChannel.close();
}
});
pipeline.addLast(new FullResponseIntercept() {
@Override
public boolean match(HttpRequest httpRequest, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
return true;
}
@Override
public void handleResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
System.out.println(httpResponse.toString());
System.out.println(httpResponse.content().toString(Charset.defaultCharset()));
}
});
I tested ok, that pipeline like this:
pipeline.addLast(FullRequestIntercept) pipeline.addLast(HttpProxyIntercept) pipeline.addLast(FullResponseIntercept)so can you try again following the above? for example:
pipeline.addLast(new FullRequestIntercept() { @Override public boolean match(HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) { return true; } }); pipeline.addLast(new HttpProxyIntercept() { @Override public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception { FullHttpRequest req = (FullHttpRequest) httpRequest; System.out.println(req.content().toString(Charset.defaultCharset())); clientChannel.close(); } }); pipeline.addLast(new FullResponseIntercept() { @Override public boolean match(HttpRequest httpRequest, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) { return true; } @Override public void handleResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) { System.out.println(httpResponse.toString()); System.out.println(httpResponse.content().toString(Charset.defaultCharset())); } });
I'll try it later, but there is another override in FullRequestIntercept:
@Override public void handleRequest(HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) {DO_SOMETHING_ON_THE_REQUEST...}.
Will post my test result later. Thx for the timely reply. @monkeyWie
@T0DDY Do you have any result?
I have test it carefully, but no luck with the result:
pipeline.addLast(FullRequestIntercept) pipeline.addFirst(HttpProxyIntercept) pipeline.addFirst(FullResponseIntercept)
the HttpProxyIntercept will not be hit, do you have any idea? @monkeyWie
@T0DDY Could you provide a simple sample code?