1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.spdy.client;
17
18 import io.netty.bootstrap.Bootstrap;
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelOption;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.MultiThreadIoEventLoopGroup;
24 import io.netty.channel.nio.NioIoHandler;
25 import io.netty.channel.socket.nio.NioSocketChannel;
26 import io.netty.handler.codec.http.DefaultFullHttpRequest;
27 import io.netty.handler.codec.http.HttpHeaderNames;
28 import io.netty.handler.codec.http.HttpHeaderValues;
29 import io.netty.handler.codec.http.HttpMethod;
30 import io.netty.handler.codec.http.HttpRequest;
31 import io.netty.handler.codec.http.HttpVersion;
32 import io.netty.handler.ssl.ApplicationProtocolConfig;
33 import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
34 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
35 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
36 import io.netty.handler.ssl.ApplicationProtocolNames;
37 import io.netty.handler.ssl.SslContext;
38 import io.netty.handler.ssl.SslContextBuilder;
39 import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
40
41
42
43
44
45
46
47
48
49 public final class SpdyClient {
50
51 static final String HOST = System.getProperty("host", "127.0.0.1");
52 static final int PORT = Integer.parseInt(System.getProperty("port", "8443"));
53
54 public static void main(String[] args) throws Exception {
55
56 final SslContext sslCtx = SslContextBuilder.forClient()
57 .trustManager(InsecureTrustManagerFactory.INSTANCE)
58 .applicationProtocolConfig(new ApplicationProtocolConfig(
59 Protocol.ALPN,
60
61 SelectorFailureBehavior.NO_ADVERTISE,
62
63 SelectedListenerFailureBehavior.ACCEPT,
64 ApplicationProtocolNames.SPDY_3_1,
65 ApplicationProtocolNames.HTTP_1_1))
66 .build();
67
68 HttpResponseClientHandler httpResponseHandler = new HttpResponseClientHandler();
69 EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
70
71 try {
72 Bootstrap b = new Bootstrap();
73 b.group(group);
74 b.channel(NioSocketChannel.class);
75 b.option(ChannelOption.SO_KEEPALIVE, true);
76 b.remoteAddress(HOST, PORT);
77 b.handler(new SpdyClientInitializer(sslCtx, httpResponseHandler));
78
79
80 Channel channel = b.connect().syncUninterruptibly().channel();
81 System.out.println("Connected to " + HOST + ':' + PORT);
82
83
84 HttpRequest request = new DefaultFullHttpRequest(
85 HttpVersion.HTTP_1_1, HttpMethod.GET, "", Unpooled.EMPTY_BUFFER);
86 request.headers().set(HttpHeaderNames.HOST, HOST);
87 request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
88
89
90 channel.writeAndFlush(request).sync();
91
92
93 httpResponseHandler.queue().take().sync();
94 System.out.println("Finished SPDY HTTP GET");
95
96
97 channel.close().syncUninterruptibly();
98 } finally {
99 group.shutdownGracefully();
100 }
101 }
102 }