Skip to content

Instantly share code, notes, and snippets.

@alfafc
Created October 12, 2019 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfafc/1279eb12ce0f11418784f632e6c7db11 to your computer and use it in GitHub Desktop.
Save alfafc/1279eb12ce0f11418784f632e6c7db11 to your computer and use it in GitHub Desktop.
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