2008-11-19 08:22:15 +01:00
|
|
|
/*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Copyright 2009 Red Hat, Inc.
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Red Hat licenses this file to you under the Apache License, version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at:
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.example.http.snoop;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-03-13 15:45:43 +01:00
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.bootstrap.ClientBootstrap;
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|
|
|
import io.netty.handler.codec.http.CookieEncoder;
|
|
|
|
import io.netty.handler.codec.http.DefaultHttpRequest;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
|
|
|
import io.netty.handler.codec.http.HttpMethod;
|
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
|
|
|
import io.netty.handler.codec.http.HttpVersion;
|
2009-03-13 11:12:51 +01:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
2009-09-25 04:26:28 +02:00
|
|
|
* A simple HTTP client that prints out the content of the HTTP response to
|
|
|
|
* {@link System#out} to test {@link HttpServer}.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2008-11-26 09:40:04 +01:00
|
|
|
public class HttpClient {
|
2008-11-26 09:39:32 +01:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
public static void main(String[] args) throws Exception {
|
2008-11-26 09:39:32 +01:00
|
|
|
if (args.length != 1) {
|
|
|
|
System.err.println(
|
2008-11-26 09:40:04 +01:00
|
|
|
"Usage: " + HttpClient.class.getSimpleName() +
|
2008-11-26 09:39:32 +01:00
|
|
|
" <URL>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI uri = new URI(args[0]);
|
|
|
|
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
|
|
|
String host = uri.getHost() == null? "localhost" : uri.getHost();
|
2010-03-31 04:26:51 +02:00
|
|
|
int port = uri.getPort();
|
|
|
|
if (port == -1) {
|
|
|
|
if (scheme.equalsIgnoreCase("http")) {
|
|
|
|
port = 80;
|
|
|
|
} else if (scheme.equalsIgnoreCase("https")) {
|
|
|
|
port = 443;
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2010-03-31 04:26:51 +02:00
|
|
|
if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) {
|
|
|
|
System.err.println("Only HTTP(S) is supported.");
|
2008-11-26 09:39:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2010-03-31 04:26:51 +02:00
|
|
|
boolean ssl = scheme.equalsIgnoreCase("https");
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
// Configure the client.
|
2009-07-23 11:05:53 +02:00
|
|
|
ClientBootstrap bootstrap = new ClientBootstrap(
|
|
|
|
new NioClientSocketChannelFactory(
|
|
|
|
Executors.newCachedThreadPool(),
|
|
|
|
Executors.newCachedThreadPool()));
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-07-23 11:05:53 +02:00
|
|
|
// Set up the event pipeline factory.
|
2010-03-31 04:26:51 +02:00
|
|
|
bootstrap.setPipelineFactory(new HttpClientPipelineFactory(ssl));
|
2008-11-26 09:39:32 +01:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
// Start the connection attempt.
|
|
|
|
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
|
|
|
|
|
|
|
|
// Wait until the connection attempt succeeds or fails.
|
|
|
|
Channel channel = future.awaitUninterruptibly().getChannel();
|
|
|
|
if (!future.isSuccess()) {
|
|
|
|
future.getCause().printStackTrace();
|
2009-07-23 11:05:53 +02:00
|
|
|
bootstrap.releaseExternalResources();
|
2008-11-26 11:18:29 +01:00
|
|
|
return;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2008-11-26 09:39:32 +01:00
|
|
|
|
2010-03-31 04:26:51 +02:00
|
|
|
// Prepare the HTTP request.
|
2008-12-01 06:27:56 +01:00
|
|
|
HttpRequest request = new DefaultHttpRequest(
|
2009-06-01 06:52:35 +02:00
|
|
|
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());
|
2009-07-23 11:05:53 +02:00
|
|
|
request.setHeader(HttpHeaders.Names.HOST, host);
|
|
|
|
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
|
2009-10-22 13:28:38 +02:00
|
|
|
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
|
2010-03-31 04:26:51 +02:00
|
|
|
|
|
|
|
// Set some example cookies.
|
2009-03-13 16:46:41 +01:00
|
|
|
CookieEncoder httpCookieEncoder = new CookieEncoder(false);
|
2009-02-17 18:13:13 +01:00
|
|
|
httpCookieEncoder.addCookie("my-cookie", "foo");
|
|
|
|
httpCookieEncoder.addCookie("another-cookie", "bar");
|
2009-07-23 11:05:53 +02:00
|
|
|
request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
|
2010-03-31 04:26:51 +02:00
|
|
|
|
|
|
|
// Send the HTTP request.
|
2008-11-27 02:11:53 +01:00
|
|
|
channel.write(request);
|
|
|
|
|
|
|
|
// Wait for the server to close the connection.
|
|
|
|
channel.getCloseFuture().awaitUninterruptibly();
|
|
|
|
|
|
|
|
// Shut down executor threads to exit.
|
2009-07-23 11:05:53 +02:00
|
|
|
bootstrap.releaseExternalResources();
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-08-28 09:15:49 +02:00
|
|
|
}
|