netty5/src/main/java/io/netty/example/http/snoop/HttpClient.java

109 lines
4.0 KiB
Java
Raw Normal View History

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
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;
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
*/
public class HttpClient {
2008-11-19 08:22:15 +01:00
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println(
"Usage: " + HttpClient.class.getSimpleName() +
" <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.");
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-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();
return;
2008-11-19 08:22:15 +01:00
}
2010-03-31 04:26:51 +02:00
// Prepare the HTTP request.
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);
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
2010-03-31 04:26:51 +02:00
// Set some example cookies.
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.
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
}