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