HTTPS using Jersey Client

后端 未结 5 1864
遇见更好的自我
遇见更好的自我 2020-11-28 21:18

How do I send GET requests using the Jersey Client API to a server which runs on the HTTPS protocol. Is there any sample code that I can use ?

5条回答
  •  心在旅途
    2020-11-28 22:05

    Waking up a dead question here but the answers provided will not work with jdk 7 (I read somewhere that a bug is open for this for Oracle Engineers but not fixed yet). Along with the link that @Ryan provided, you will have to also add :

    System.setProperty("jsse.enableSNIExtension", "false");

    (Courtesy to many stackoverflow answers combined together to figure this out)

    The complete code will look as follows which worked for me (without setting the system property the Client Config did not work for me):

    import java.security.SecureRandom;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.config.ClientConfig;
    import com.sun.jersey.api.client.config.DefaultClientConfig;
    import com.sun.jersey.client.urlconnection.HTTPSProperties;
    public class ClientHelper
    {
        public static ClientConfig configureClient()
        {
            System.setProperty("jsse.enableSNIExtension", "false");
            TrustManager[] certs = new TrustManager[]
            {
                new X509TrustManager()
                {
                    @Override
                    public X509Certificate[] getAcceptedIssuers()
                    {
                        return null;
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType)
                            throws CertificateException
                    {
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType)
                            throws CertificateException
                    {
                    }
                }
            };
            SSLContext ctx = null;
            try
            {
                ctx = SSLContext.getInstance("SSL");
                ctx.init(null, certs, new SecureRandom());
            }
            catch (java.security.GeneralSecurityException ex)
            {
            }
            HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
            ClientConfig config = new DefaultClientConfig();
            try
            {
                config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
                        new HostnameVerifier()
                {
                    @Override
                    public boolean verify(String hostname, SSLSession session)
                    {
                        return true;
                    }
                },
                        ctx));
            }
            catch (Exception e)
            {
            }
            return config;
        }
    
        public static Client createClient()
        {
            return Client.create(ClientHelper.configureClient());
        }
    

提交回复
热议问题