Java tutorial
/* * Copyright 2012 The Netty Project * * The Netty Project 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: * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. */ package nettyTest.httpSsl.oneWay; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.codec.http.cookie.ClientCookieEncoder; import io.netty.handler.codec.http.cookie.DefaultCookie; import io.netty.handler.ssl.util.FingerprintTrustManagerFactory; import io.netty.handler.ssl.util.SimpleTrustManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import java.io.File; import java.net.URI; import java.security.KeyStore; public final class HttpsHelloWorldClient { //static final String URL = System.getProperty("url", "https://backend-server.com/hello"); //static final String URL = System.getProperty("url", "https://www.vultr.com/"); //static final String URL = System.getProperty("url", "https://www.google.com"); //static final String URL = System.getProperty("url", "https://www.taobao.com/"); //static final String URL = System.getProperty("url", "https://www.jd.com/"); //static final String URL = System.getProperty("url", "https://www.qq.com/"); //static final String URL = System.getProperty("url", "https://www.ehomepay.com.cn/"); //static final String URL = System.getProperty("url", "https://netty.io/"); //static final String URL = System.getProperty("url", "https://mail.google.com"); //static final String URL = System.getProperty("url", "https://www.baidu.com"); static final String URL = System.getProperty("url", "https://netty.io/wiki/forked-tomcat-native.html"); //static final String URL = System.getProperty("url", "https://ocean-core.bkjk-inc.com/#/applications/osg/clusters?reg=test&stack=teslagateway"); 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) { File certificate = new File( HttpsHelloWorldServer.class.getClassLoader().getResource("nettyca/google.crt").getFile()); //TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); //TrustManagerFactory instance = SimpleTrustManagerFactory.getInstance(SimpleTrustManagerFactory.getDefaultAlgorithm()); TrustManagerFactory instance = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); //TrustManagerFactory instance = FingerprintTrustManagerFactory.getInstance(FingerprintTrustManagerFactory.getDefaultAlgorithm()); KeyStore keyStore = null; instance.init(keyStore); TrustManager[] tms = instance.getTrustManagers(); //instance.init(null); sslCtx = SslContextBuilder.forClient().trustManager(instance).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .handler(new HttpsHelloWorldClientInitializer(sslCtx, host, port)); // 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.LAX .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(); } } }