HTTPS support
This commit is contained in:
parent
7c139cf1b7
commit
c1a70b3579
@ -53,15 +53,22 @@ public class HttpClient {
|
||||
URI uri = new URI(args[0]);
|
||||
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
||||
String host = uri.getHost() == null? "localhost" : uri.getHost();
|
||||
int port = uri.getPort() == -1? 80 : uri.getPort();
|
||||
int port = uri.getPort();
|
||||
if (port == -1) {
|
||||
if (scheme.equalsIgnoreCase("http")) {
|
||||
port = 80;
|
||||
} else if (scheme.equalsIgnoreCase("https")) {
|
||||
port = 443;
|
||||
}
|
||||
}
|
||||
|
||||
if (!scheme.equals("http")) {
|
||||
// We can actually support HTTPS fairly easily by inserting
|
||||
// an SslHandler to the pipeline - left as an exercise.
|
||||
System.err.println("Only HTTP is supported.");
|
||||
if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) {
|
||||
System.err.println("Only HTTP(S) is supported.");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean ssl = scheme.equalsIgnoreCase("https");
|
||||
|
||||
// Configure the client.
|
||||
ClientBootstrap bootstrap = new ClientBootstrap(
|
||||
new NioClientSocketChannelFactory(
|
||||
@ -69,7 +76,7 @@ public class HttpClient {
|
||||
Executors.newCachedThreadPool()));
|
||||
|
||||
// Set up the event pipeline factory.
|
||||
bootstrap.setPipelineFactory(new HttpClientPipelineFactory());
|
||||
bootstrap.setPipelineFactory(new HttpClientPipelineFactory(ssl));
|
||||
|
||||
// Start the connection attempt.
|
||||
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
|
||||
@ -82,16 +89,20 @@ public class HttpClient {
|
||||
return;
|
||||
}
|
||||
|
||||
// Send the HTTP request.
|
||||
// Prepare the HTTP request.
|
||||
HttpRequest request = new DefaultHttpRequest(
|
||||
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());
|
||||
request.setHeader(HttpHeaders.Names.HOST, host);
|
||||
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
|
||||
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
|
||||
|
||||
// Set some example cookies.
|
||||
CookieEncoder httpCookieEncoder = new CookieEncoder(false);
|
||||
httpCookieEncoder.addCookie("my-cookie", "foo");
|
||||
httpCookieEncoder.addCookie("another-cookie", "bar");
|
||||
request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
|
||||
|
||||
// Send the HTTP request.
|
||||
channel.write(request);
|
||||
|
||||
// Wait for the server to close the connection.
|
||||
|
@ -17,11 +17,14 @@ package org.jboss.netty.example.http.snoop;
|
||||
|
||||
import static org.jboss.netty.channel.Channels.*;
|
||||
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
import org.jboss.netty.channel.ChannelPipeline;
|
||||
import org.jboss.netty.channel.ChannelPipelineFactory;
|
||||
import org.jboss.netty.example.securechat.SecureChatSslContextFactory;
|
||||
import org.jboss.netty.handler.codec.http.HttpClientCodec;
|
||||
import org.jboss.netty.handler.codec.http.HttpContentDecompressor;
|
||||
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
|
||||
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
||||
import org.jboss.netty.handler.ssl.SslHandler;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
@ -31,15 +34,34 @@ import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
public class HttpClientPipelineFactory implements ChannelPipelineFactory {
|
||||
|
||||
private final boolean ssl;
|
||||
|
||||
public HttpClientPipelineFactory(boolean ssl) {
|
||||
this.ssl = ssl;
|
||||
}
|
||||
|
||||
public ChannelPipeline getPipeline() throws Exception {
|
||||
// Create a default pipeline implementation.
|
||||
ChannelPipeline pipeline = pipeline();
|
||||
pipeline.addLast("decoder", new HttpResponseDecoder());
|
||||
|
||||
// Enable HTTPS if necessary.
|
||||
if (ssl) {
|
||||
SSLEngine engine =
|
||||
SecureChatSslContextFactory.getClientContext().createSSLEngine();
|
||||
engine.setUseClientMode(true);
|
||||
|
||||
pipeline.addLast("ssl", new SslHandler(engine));
|
||||
}
|
||||
|
||||
pipeline.addLast("codec", new HttpClientCodec());
|
||||
|
||||
// Remove the following line if you don't want automatic content decompression.
|
||||
pipeline.addLast("inflater", new HttpContentDecompressor());
|
||||
|
||||
// Uncomment the following line if you don't want to handle HttpChunks.
|
||||
//pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
|
||||
pipeline.addLast("encoder", new HttpRequestEncoder());
|
||||
|
||||
pipeline.addLast("handler", new HttpResponseHandler());
|
||||
return pipeline;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user