io.netty.handler.codec.http.ClientCookieEncoder Java Examples

The following examples show how to use io.netty.handler.codec.http.ClientCookieEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NettyHttpRequest.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private void addCookies(Map<String, String> cookies) {
   if (cookies != null && cookies.size() > 0) {
      List<Cookie> cookieList = new ArrayList<>();
      for (String name : cookies.keySet()) {
         cookieList.add(new DefaultCookie(name, cookies.get(name)));
      }
      request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(cookieList));
   }
}
 
Example #2
Source File: HttpUploadClient.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 **/
private static List<Entry<String, String>> formget(
        Bootstrap bootstrap, String host, int port, String get, URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();

    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue ���&");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");

    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaders.Names.HOST, host);
    headers.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    headers.set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP + ',' + HttpHeaders.Values.DEFLATE);

    headers.set(HttpHeaders.Names.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaders.Names.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaders.Names.REFERER, uriSimple.toString());
    headers.set(HttpHeaders.Names.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaders.Names.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    //connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");

    headers.set(
            HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(
                    new DefaultCookie("my-cookie", "foo"),
                    new DefaultCookie("another-cookie", "bar"))
    );

    // send request
    List<Entry<String, String>> entries = headers.entries();
    channel.writeAndFlush(request);

    // Wait for the server to close the connection.
    channel.closeFuture().sync();

    return entries;
}
 
Example #3
Source File: HttpSnoopClient.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null? "http" : uri.getScheme();
    String host = uri.getHost() == null? "127.0.0.1" : uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(NioSocketChannel.class)
         .handler(new HttpSnoopClientInitializer(sslCtx));

        // Make the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        // Prepare the HTTP request.
        HttpRequest request = new DefaultFullHttpRequest(
                HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
        request.headers().set(HttpHeaders.Names.HOST, host);
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        // Set some example cookies.
        request.headers().set(
                HttpHeaders.Names.COOKIE,
                ClientCookieEncoder.encode(
                        new DefaultCookie("my-cookie", "foo"),
                        new DefaultCookie("another-cookie", "bar")));

        // Send the HTTP request.
        ch.writeAndFlush(request);

        // Wait for the server to close the connection.
        ch.closeFuture().sync();
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
    }
}
 
Example #4
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaderNames.HOST, host);
		headers.set(HttpHeaderNames.CONNECTION,HttpHeaderValues.CLOSE);
		headers.set(HttpHeaderNames.ACCEPT_ENCODING,HttpHeaderValues.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaderNames.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #5
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #6
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #7
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #8
Source File: NettyServerUtil.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #9
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #10
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #11
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #12
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}
 
Example #13
Source File: BootstrapTemplate.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{
	URI uri = new URI(url);
	String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
	String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
	int port = uri.getPort();
	if (port == -1) {
		if ("http".equalsIgnoreCase(scheme)) {
			port = 80;
		} else if ("https".equalsIgnoreCase(scheme)) {
			port = 443;
		}
	}

	if (!"http".equalsIgnoreCase(scheme)
			&& !"https".equalsIgnoreCase(scheme)) {
		System.err.println("Only HTTP(S) is supported.");
		return;
	}

	// Configure SSL context if necessary.
	final boolean ssl = "https".equalsIgnoreCase(scheme);
	final SslContext sslCtx;
	if (ssl) {
		sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
	} else {
		sslCtx = null;
	}

	// Configure the client.
	EventLoopGroup group = new NioEventLoopGroup();
	try {
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class)
				.handler(new HttpDownloadertInitializer(sslCtx,handler));
		// Make the connection attempt.
		Channel ch = b.connect(host, port).sync().channel();
		// Prepare the HTTP request.
		HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
		HttpHeaders headers = request.headers();
		headers.set(HttpHeaders.Names.HOST, host);
		headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE);
		headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP);
		// Set some example cookies.
		headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo")));
		ch.writeAndFlush(request);
		// Wait for the server to close the connection.
		ch.closeFuture().sync();
		Thread.sleep(1000);
	} finally {
		// Shut down executor threads to exit.
		group.shutdownGracefully();
	}	
}