1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.http.snoop;
17
18 import java.net.InetSocketAddress;
19 import java.net.URI;
20 import java.util.concurrent.Executors;
21
22 import org.jboss.netty.bootstrap.ClientBootstrap;
23 import org.jboss.netty.channel.Channel;
24 import org.jboss.netty.channel.ChannelFuture;
25 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
26 import org.jboss.netty.handler.codec.http.CookieEncoder;
27 import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
28 import org.jboss.netty.handler.codec.http.HttpHeaders;
29 import org.jboss.netty.handler.codec.http.HttpMethod;
30 import org.jboss.netty.handler.codec.http.HttpRequest;
31 import org.jboss.netty.handler.codec.http.HttpVersion;
32
33
34
35
36
37 public class HttpSnoopClient {
38
39 private final URI uri;
40
41 public HttpSnoopClient(URI uri) {
42 this.uri = uri;
43 }
44
45 public void run() {
46 String scheme = uri.getScheme() == null? "http" : uri.getScheme();
47 String host = uri.getHost() == null? "localhost" : uri.getHost();
48 int port = uri.getPort();
49 if (port == -1) {
50 if (scheme.equalsIgnoreCase("http")) {
51 port = 80;
52 } else if (scheme.equalsIgnoreCase("https")) {
53 port = 443;
54 }
55 }
56
57 if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) {
58 System.err.println("Only HTTP(S) is supported.");
59 return;
60 }
61
62 boolean ssl = scheme.equalsIgnoreCase("https");
63
64
65 ClientBootstrap bootstrap = new ClientBootstrap(
66 new NioClientSocketChannelFactory(
67 Executors.newCachedThreadPool(),
68 Executors.newCachedThreadPool()));
69
70
71 bootstrap.setPipelineFactory(new HttpSnoopClientPipelineFactory(ssl));
72
73
74 ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
75
76
77 Channel channel = future.awaitUninterruptibly().getChannel();
78 if (!future.isSuccess()) {
79 future.getCause().printStackTrace();
80 bootstrap.releaseExternalResources();
81 return;
82 }
83
84
85 HttpRequest request = new DefaultHttpRequest(
86 HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
87 request.setHeader(HttpHeaders.Names.HOST, host);
88 request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
89 request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
90
91
92 CookieEncoder httpCookieEncoder = new CookieEncoder(false);
93 httpCookieEncoder.addCookie("my-cookie", "foo");
94 httpCookieEncoder.addCookie("another-cookie", "bar");
95 request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
96
97
98 channel.write(request);
99
100
101 channel.getCloseFuture().awaitUninterruptibly();
102
103
104 bootstrap.releaseExternalResources();
105 }
106
107 public static void main(String[] args) throws Exception {
108 if (args.length != 1) {
109 System.err.println(
110 "Usage: " + HttpSnoopClient.class.getSimpleName() +
111 " <URL>");
112 return;
113 }
114
115 URI uri = new URI(args[0]);
116 new HttpSnoopClient(uri).run();
117 }
118 }