Guest User

Untitled

a guest
Aug 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Executors;
  3.  
  4. import org.jboss.netty.bootstrap.ClientBootstrap;
  5. import org.jboss.netty.buffer.ChannelBuffer;
  6. import org.jboss.netty.buffer.ChannelBuffers;
  7. import org.jboss.netty.channel.ChannelHandlerContext;
  8. import org.jboss.netty.channel.ChannelPipeline;
  9. import org.jboss.netty.channel.ChannelPipelineFactory;
  10. import org.jboss.netty.channel.Channels;
  11. import org.jboss.netty.channel.ExceptionEvent;
  12. import org.jboss.netty.channel.MessageEvent;
  13. import org.jboss.netty.channel.SimpleChannelHandler;
  14. import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
  15. import org.jboss.netty.handler.codec.base64.Base64;
  16. import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
  17. import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
  18. import org.jboss.netty.handler.codec.http.HttpClientCodec;
  19. import org.jboss.netty.handler.codec.http.HttpHeaders;
  20. import org.jboss.netty.handler.codec.http.HttpMethod;
  21. import org.jboss.netty.handler.codec.http.HttpResponse;
  22. import org.jboss.netty.handler.codec.http.HttpVersion;
  23. import org.jboss.netty.util.CharsetUtil;
  24.  
  25. public class BasicAuthTest {
  26. private static final int PORT = 80;
  27. private static final String USERNAME = "";
  28. private static final String PASSWORD = "";
  29. private static final String URI = "";
  30. private static final String HOST = "";
  31.  
  32. public static void main(String[] args) {
  33.  
  34. ClientBootstrap client = new ClientBootstrap(
  35. new NioClientSocketChannelFactory(
  36. Executors.newCachedThreadPool(),
  37. Executors.newCachedThreadPool()));
  38.  
  39. client.setPipelineFactory(new ChannelPipelineFactory() {
  40.  
  41. @Override
  42. public ChannelPipeline getPipeline() throws Exception {
  43. ChannelPipeline pipeline = Channels.pipeline();
  44. pipeline.addLast("codec", new HttpClientCodec());
  45. pipeline.addLast("aggregator", new HttpChunkAggregator(5242880));
  46. pipeline.addLast("authHandler", new ClientMessageHandler());
  47. return pipeline;
  48. }
  49. });
  50.  
  51. DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, URI);
  52.  
  53. request.addHeader(HttpHeaders.Names.HOST, HOST);
  54.  
  55. String authString = USERNAME + ":" + PASSWORD;
  56. ChannelBuffer authChannelBuffer = ChannelBuffers.copiedBuffer(authString, CharsetUtil.UTF_8);
  57. ChannelBuffer encodedAuthChannelBuffer = Base64.encode(authChannelBuffer);
  58. request.addHeader(HttpHeaders.Names.AUTHORIZATION, encodedAuthChannelBuffer.toString(CharsetUtil.UTF_8));
  59.  
  60. client.connect(new InetSocketAddress(HOST, PORT)).awaitUninterruptibly().getChannel()
  61. .write(request).awaitUninterruptibly();
  62.  
  63. }
  64.  
  65. public static class ClientMessageHandler extends SimpleChannelHandler {
  66. @Override
  67. public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
  68. e.getCause().printStackTrace();
  69. }
  70.  
  71. @Override
  72. public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
  73. HttpResponse httpResponse = (HttpResponse) e.getMessage();
  74. String json = httpResponse.getContent().toString(CharsetUtil.UTF_8);
  75. System.out.println(json);
  76. }
  77. }
  78.  
  79. }
Add Comment
Please, Sign In to add comment