本文整理汇总了Java中io.netty.handler.ssl.SslContext.newClientContext方法的典型用法代码示例。如果您正苦于以下问题:Java SslContext.newClientContext方法的具体用法?Java SslContext.newClientContext怎么用?Java SslContext.newClientContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.ssl.SslContext
的用法示例。
在下文中一共展示了SslContext.newClientContext方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initChannel
import io.netty.handler.ssl.SslContext; //导入方法依赖的package包/类
@Override
protected void initChannel(SocketChannel channel) throws SSLException {
URI uri = config.getConnectionWebsocketUri();
DefaultHttpHeaders headers = new DefaultHttpHeaders();
headers.add(USER_ID_HEADER, config.getConnectionUserId().toString());
headers.add(USER_PASSWORD_HEADER, config.getConnectionUserPassword());
headers.add(SUPPLIER_ID_HEADER, config.getConnectionServerId());
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WS_VERSION, null, false, headers);
ChannelPipeline pipeline = channel.pipeline();
if (config.isConnectionSecure()) {
try {
SslContext sslContext = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
pipeline.addLast(sslContext.newHandler(channel.alloc()));
} catch (SSLException e) {
logger.log(Level.SEVERE, "Shutting down client due to unexpected failure to create SSL context", e);
throw e;
}
}
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpObjectAggregator(8192));
pipeline.addLast(new AudioConnectClientHandler(handshaker));
}
示例2: connect
import io.netty.handler.ssl.SslContext; //导入方法依赖的package包/类
@Override
protected InetSocketAddress connect(final URI uri, final int port) throws Exception {
InetSocketAddress address = super.connect(uri, port);
final SslContext ssl;
if (uri.getScheme().equalsIgnoreCase("wss")) {
ssl = SslContext.newClientContext(getTrustManager());
} else {
ssl = null;
}
HttpHeaders headers = new DefaultHttpHeaders();
for (Map.Entry<String, String> header : this.headers.entrySet()) {
headers.add(header.getKey(), header.getValue());
}
clientListener = new ClientListener(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, headers));
Bootstrap b = new Bootstrap();
b.group(SocketPoller.group())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (ssl != null) p.addLast(ssl.newHandler(ch.alloc(), uri.getHost(), port));
p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), clientListener);
}
});
channelFuture = b.connect(address);
return address;
}
示例3: get
import io.netty.handler.ssl.SslContext; //导入方法依赖的package包/类
private File get(URI uri, String url, String proxyHost, int proxyPort,
final String target) throws Exception {
final SslContext sslCtx;
if (isSecured(uri)) {
sslCtx = SslContext
.newClientContext(InsecureTrustManagerFactory.INSTANCE);
} else {
sslCtx = null;
}
final NettyClientHandler handler = new NettyClientHandler(target);
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.handler(new NettyClientInitializer(sslCtx, handler));
// .handler(new HttpSnoopClientInitializer(sslCtx));
Channel ch = b.connect(proxyHost, proxyPort).sync().channel();
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, url);
request.headers().set(HttpHeaders.Names.HOST, uri.getHost());
request.headers().set(HttpHeaders.Names.CONNECTION,
HttpHeaders.Values.CLOSE);
request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING,
HttpHeaders.Values.GZIP);
ch.writeAndFlush(request);
ch.closeFuture().sync();
} finally {
group.shutdownGracefully();
}
return handler.getFile();
}
示例4: get
import io.netty.handler.ssl.SslContext; //导入方法依赖的package包/类
public File get(String url) throws Exception {
URI uri = new URI(url);
String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
if (!"http".equalsIgnoreCase(scheme)
&& !"https".equalsIgnoreCase(scheme)) {
LOG.info("Only HTTP(S) is supported.");
return null;
}
String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
int port;
if (uri.getPort() == -1) {
if ("http".equalsIgnoreCase(scheme)) {
port = 80;
} else if ("https".equalsIgnoreCase(scheme)) {
port = 443;
} else {
port = -1;
}
} else {
port = uri.getPort();
}
this.uri = new URI(scheme, uri.getUserInfo(), host, port,
uri.getPath(), uri.getQuery(), uri.getFragment());
final boolean ssl = "https".equalsIgnoreCase(scheme);
if (ssl) {
sslCtx = SslContext
.newClientContext(InsecureTrustManagerFactory.INSTANCE);
} else {
sslCtx = null;
}
connect(false, group);
return null;
}
示例5: tryConnect
import io.netty.handler.ssl.SslContext; //导入方法依赖的package包/类
public boolean tryConnect(boolean ssl, SimpleChannelInboundHandler<Packet> default_handler, Auth auth, Runnable cancelTask)
{
try
{
eventLoopGroup = NetworkUtils.eventLoopGroup(4);
if (ssl) sslContext = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
Bootstrap bootstrap = new Bootstrap()
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.IP_TOS, 24)
.option(ChannelOption.AUTO_READ, true)
.group(eventLoopGroup)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception
{
if (sslContext != null)
channel.pipeline().addLast(sslContext.newHandler(channel.alloc(), connectableAddress.getHostName(), connectableAddress.getPort()));
NetworkUtils.initChannel(channel).pipeline().addLast(default_handler);
}
})
.channel(NetworkUtils.socketChannel());
this.channel = bootstrap.connect(connectableAddress.getHostName(), connectableAddress.getPort()).sync().channel().writeAndFlush(new PacketOutAuth(auth)).syncUninterruptibly().channel();
return true;
} catch (Exception ex)
{
connectionTrys++;
System.out.println("Failed to connect... [" + connectionTrys + "]");
System.out.println("Error: " + ex.getMessage());
if(eventLoopGroup != null)
eventLoopGroup.shutdownGracefully();
eventLoopGroup = null;
if (cancelTask != null)
{
cancelTask.run();
}
return false;
}
}