Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Java Quarkus GET Request Disable SSl

19 Oct 2021 »

examples https://docs.jboss.org/resteasy/docs/3.5.1.Final/userguide/html/RESTEasy_Client_Framework.html

https://stackoverflow.com/questions/6047996/ignore-self-signed-ssl-cert-using-jersey-client

ClientBuilder

@GET
@Path("/2")
@Produces(MediaType.TEXT_PLAIN)
public String test2() throws Exception{

    SSLContext sslcontext = SSLContext.getInstance("TLS");

    sslcontext.init(null, new TrustManager[]{new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
    }}, new java.security.SecureRandom());

//        Client client = ClientBuilder.newClient()
    Client client = ClientBuilder.newBuilder()
                            .sslContext(sslcontext)
                            .hostnameVerifier((s1, s2) -> true)
                            .build();
    String response = client.target("https://nylen.io/d3-spirograph/")
            //.queryParam("query", "q")
            .request()
            .accept("text/html")
            .get(String.class);
            //.post(Entity.entity("e", "text/plain"), String.class);
    client.close();
    return response;
}

httpclients

@GET
@Path("/1")
@Produces(MediaType.TEXT_PLAIN)
public String test1(){
    try {
        CloseableHttpClient httpClient = HttpClients
                .custom()
                .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
                .build();
        HttpGet request = new HttpGet("https://nylen.io/d3-spirograph/");
        CloseableHttpResponse response = httpClient.execute(request);
        System.out.println(response.getProtocolVersion());              // HTTP/1.1
        System.out.println(response.getStatusLine().getStatusCode());   // HTTP/1.1
        System.out.println(response.getStatusLine().getReasonPhrase()); // OK
        System.out.println(response.getStatusLine().toString());        // HTTP/1.1 200 OK

        HttpEntity entity = response.getEntity();
        if (entity != null){
            String result = EntityUtils.toString(entity);
            response.close();
            return result;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    return "ok";
}