Java 如何在Netty客户端中使用Socks4/5代理处理程序(4.1)

Java 如何在Netty客户端中使用Socks4/5代理处理程序(4.1),java,httpclient,netty,http-proxy,socks,Java,Httpclient,Netty,Http Proxy,Socks,我需要在Netty客户端中配置socks代理(通过socks4或5代理请求不同的站点)。 尝试了许多免费袜子列表中的代理(如www.socks-proxy.net等),但没有成功: @Test public void testProxy() throws Exception { final String ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0

我需要在Netty客户端中配置socks代理(通过socks4或5代理请求不同的站点)。 尝试了许多免费袜子列表中的代理(如www.socks-proxy.net等),但没有成功:

@Test
public void testProxy() throws Exception {
    final String ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
    final String host = "www.main.de";
    final int port = 80;

    Bootstrap b = new Bootstrap();
    b.group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();

                    p.addLast(new HttpClientCodec());
                    p.addLast(new HttpContentDecompressor());
                    p.addLast(new HttpObjectAggregator(10_485_760));
                    p.addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                            HttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
                            request.headers().set(HOST, host + ":" + port);
                            request.headers().set(USER_AGENT, ua);
                            request.headers().set(CONNECTION, CLOSE);

                            ctx.writeAndFlush(request);

                            System.out.println("!sent");
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            System.out.println("!answer");
                            if (msg instanceof FullHttpResponse) {
                                FullHttpResponse httpResp = (FullHttpResponse) msg;


                                ByteBuf content = httpResp.content();
                                String strContent = content.toString(UTF_8);
                                System.out.println("body: " + strContent);

                                finish.countDown();
                                return;
                            }

                            super.channelRead(ctx, msg);
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                            cause.printStackTrace(System.err);
                            ctx.close();
                            finish.countDown();
                        }
                    });

                    p.addLast(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678)));
                }
            });

    b.connect(host, port).awaitUninterruptibly();
    System.out.println("!connected");

    finish.await(1, MINUTES);
}
@测试
public void testProxy()引发异常{
最终字符串ua=“Mozilla/5.0(Windows NT 6.1)AppleWebKit/537.36(KHTML,如Gecko)Chrome/41.0.2228.0 Safari/537.36”;
最后一个字符串host=“www.main.de”;
最终int端口=80;
引导b=新引导();
b、 组(新的NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(新的通道初始值设定项(){
@凌驾
受保护的void initChannel(SocketChannel ch)引发异常{
ChannelPipeline p=通道管道();
p、 addLast(新的HttpClientCodec());
p、 addLast(新的HttpContentDecompressor());
p、 addLast(新的HttpObjectAggregator(10_485_760));
p、 addLast(新ChannelInboundHandlerAdapter(){
@凌驾
公共无效channelActive(最终ChannelHandlerContext ctx)引发异常{
HttpRequest请求=新的DefaultFullHttpRequest(HTTP_1_1,GET,“/”;
request.headers().set(主机,主机+:“+端口);
request.headers().set(用户\代理,ua);
request.headers().set(连接,关闭);
ctx.writeAndFlush(请求);
System.out.println(“!sent”);
}
@凌驾
public void channelRead(ChannelHandlerContext ctx,Object msg)引发异常{
System.out.println(“!answer”);
if(msg instanceof FullHttpResponse){
FullHttpResponse httpResp=(FullHttpResponse)msg;
ByteBuf content=httpResp.content();
字符串strContent=content.toString(UTF_8);
System.out.println(“正文:+strContent”);
完成。倒计时();
返回;
}
超级通道读取(ctx、msg);
}
@凌驾
public void exceptionCaught(ChannelHandlerContext ctx,可丢弃原因)引发异常{
原因.printStackTrace(系统错误);
ctx.close();
完成。倒计时();
}
});
p、 addLast(新的Socks4ProxyHandler(新的InetSocketAddress(“149.202.68.167”,37678));
}
});
b、 连接(主机、端口)。不间断地等待();
System.out.println(“!connected”);
完成。等待(1分钟);
}
连接挂起、重置或出现一些奇怪的异常。 发生了什么?
从4.1开始,Netty就增加了代理支持(现在有了4.1CR,试用过,以前有4.1b7-8)

代理实例应该是管道中的第一个实例,因为您希望它在处理任何http内容之前先处理到代理的连接

要更改此项,请更改
p.addLast(新的Socks4ProxyHandler(新的InetSocketAddress(“149.202.68.167”,37678))至:

p.addFirst(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678)));
如的文档中所述,数据流从第一个处理程序开始,到最后一个处理程序结束