HTTPS support..New to netty

keerthik keerthi.kamalapuri at yellowbook.com
Wed Mar 24 21:34:05 EDT 2010


Hi Jon ,

As suggested by you, I modified to code to look like the code in http snoop.

public class HttpClient {

    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("www.myhost.com");
        String scheme = uri.getScheme() == null? "https" : uri.getScheme();
        String host = uri.getHost() == null? "localhost" : uri.getHost();
        int port = uri.getPort() == -1? 443 : uri.getPort();

       // 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.");
         //   return;
        //}

        // Configure the client.
        ClientBootstrap bootstrap = new ClientBootstrap(
                new NioClientSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        // Set up the event pipeline factory.
        bootstrap.setPipelineFactory(new HttpClientPipelineFactory());

        // 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();
            bootstrap.releaseExternalResources();
            return;
        }

        // Send 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);
        CookieEncoder httpCookieEncoder = new CookieEncoder(false);
        httpCookieEncoder.addCookie("my-cookie", "foo");
        httpCookieEncoder.addCookie("another-cookie", "bar");
        request.setHeader(HttpHeaders.Names.COOKIE,
httpCookieEncoder.encode());
        channel.write(request);

        // Wait for the server to close the connection.
        channel.getCloseFuture().awaitUninterruptibly();

        // Shut down executor threads to exit.
        bootstrap.releaseExternalResources();
    }
}


public class HttpClientPipelineFactory implements ChannelPipelineFactory {
    public ChannelPipeline getPipeline() throws Exception {
        // Create a default pipeline implementation.
        ChannelPipeline pipeline = pipeline();
        
        SSLEngine engine;
       // if (handler instanceof SecureChatClientHandler) {
            engine =
SecureChatSslContextFactory.getClientContext().createSSLEngine();
            engine.setUseClientMode(true);
        /*} else {
            engine =
SecureChatSslContextFactory.getServerContext().createSSLEngine();
            engine.setUseClientMode(false);
        }*/

        pipeline.addLast("ssl", new SslHandler(engine));

        // On top of the SSL handler, add the text line codec.
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
                8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new HttpResponseDecoder());
        // 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;
    }
}

This doesn't work..All I want to do is to connect to real https website and
read data. I don't care (atleast at this point) about ssl server implemented
by netty.

Can u please paste a snippet of ur code so that I can get to see how netty
works. Sorry, I was really new to netty and also SSL concept. A snippet
which receives data from real https website would be greatly helpful. Thank
you for your quick response.

Keerthi

-- 
View this message in context: http://n2.nabble.com/HTTPS-support-New-to-netty-tp4793904p4795110.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list