PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
HttpClient httpClient = HttpClients.custom()
.setConnectionManager(connManager)
.build();
public class HttpClientSingleton {
private static HttpClient instance;
private HttpClientSingleton() {}
public static synchronized HttpClient getInstance() {
if (instance == null) {
instance = HttpClientBuilder.create().build();
}
return instance;
}
}
RequestConfig requestConfig = RequestConfig.custom()
.build();
HttpGet httpGet = new HttpGet("http://example.com");
httpGet.setConfig(requestConfig);
HttpPost httpPost = new HttpPost("http://example.com");
httpPost.setHeader("Accept-Encoding", "gzip");
httpPost.setHeader("Cache-Control", "max-age=3600");
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new HttpObjectCodec());
}
});
Channel channel = bootstrap.connect("example.com", 80).sync().channel();