Skip to content

Instantly share code, notes, and snippets.

@alfafc
Created Oct 12, 2019
Embed
What would you like to do?
Example to avoid runtime error: javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name.java
package com.example;
import com.m.arch.utils.NoSNISSLSocketFactory;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public final class App {
public static void main(String[] args) throws IOException {
final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
final int timeoutSeconds = 2;
httpClientBuilder.setDefaultRequestConfig(getDefaultConfig(timeoutSeconds * 1000));
httpClientBuilder.disableAutomaticRetries();
httpClientBuilder.setSSLSocketFactory(new NoSNISSLSocketFactory());
final HttpPost request = new HttpPost("url");
request.addHeader("Content-Type", "application/json; charset=utf-8");
request.setEntity(new StringEntity("content", StandardCharsets.UTF_8));
httpClientBuilder.build().execute(request);
}
private static RequestConfig getDefaultConfig(final int timeoutInMillis) {
return RequestConfig.custom().
setSocketTimeout(timeoutInMillis).
setConnectTimeout(timeoutInMillis).
setConnectionRequestTimeout(timeoutInMillis).
build();
}
}
package com.m.arch.utils;
import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.protocol.HttpContext;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
public class NoSNISSLSocketFactory extends SSLSocketFactory implements LayeredConnectionSocketFactory {
private final SSLSocketFactory sslSocketFactory;
NoSNISSLSocketFactory() {
this.sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
}
@Override
public String[] getDefaultCipherSuites() {
return sslSocketFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return sslSocketFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
return sslSocketFactory.createSocket(socket, "", port, autoClose);
}
@Override
public Socket createSocket(String host, int port) throws IOException {
return createSocket(new Socket(host, port), host, port, true);
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
return createSocket(new Socket(host, port, localHost, localPort), host, port, true);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return sslSocketFactory.createSocket(host, port);
}
@Override
public Socket createSocket(InetAddress host, int port, InetAddress localHost, int localPort) throws IOException {
return createSocket(new Socket(host, port, localHost, localPort), host.getHostName(), port, true);
}
@Override
public Socket createLayeredSocket(Socket socket, String s, int i, HttpContext httpContext) {
return socket;
}
@Override
public Socket createSocket(HttpContext httpContext) throws IOException {
final HttpHost targetHost = ((HttpClientContext) httpContext).getTargetHost();
return createSocket(targetHost.getHostName(), 443);
}
@Override
public Socket connectSocket(int i, Socket socket, HttpHost httpHost, InetSocketAddress inetSocketAddress, InetSocketAddress inetSocketAddress1, HttpContext httpContext) {
return socket;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment