URL/URLConnection
Java is fundamentally different than js.
One of these differences is that java is blocking/synchronous meaning the default way is to wait for the request:
known/trusted certificate or HTTP
final URL url=new URL("url here");//create url
//Open an InputStream (binary) to the URL
//Convert it to a reader(text)
//Use buffering
try(BufferedReader br=new BufferedReader (new InputStreamReader(url.openStream(),StandardCharsets.UTF_8))){
br.lines().forEach(System.out::println);//print all lines
}catch(IOException e){
//Error handling
}
The try-with-resources statement closes the resources automatically when they are not needed any more.
This is important to do this so you don't have resource leaks.
Self signed/custom certificate
As stated in this answer, you can configure a TrustStore
in order to add a custom certificate:
//Load certificate
File crtFile = new File("server.crt");
Certificate certificate = CertificateFactory.getInstance("X.509").generateCertificate(new FileInputStream(crtFile));
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("server", certificate);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
final URL url=new URL("url here");//create url
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();//open a connection
connection.setSSLSocketFactory(sslContext.getSocketFactory());
//Open an InputStream (binary) to the URL
//Convert it to a reader(text)
//Use buffering
try(BufferedReader br=new BufferedReader (new InputStreamReader(connection.getInputStream(),StandardCharsets.UTF_8))){
br.lines().forEach(System.out::println);//print all lines
}catch(IOException e){
//Error handling
}
HttpClient
If you use Java 11 or newer, you could also try to use the asynchronous HttpClient
as described here.
This is more similar to the JS approach.
known/trusted certificate or HTTP
HttpClient client = HttpClient.newHttpClient();//create the HTTPClient
HttpRequest request = HttpRequest.newBuilder()//create a request object, GET is the default
.uri(URI.create("url here"))//set the url
.build();//build the request object
client.sendAsync(request, BodyHandlers.ofString())//send the request in a background thread (fetch() in your code)
.thenApply(HttpResponse::body)//get the body as a string (.then(r=>r.text()) in your code))
.thenAccept(System.out::println);//print it (.then(console.log) in your code)
This approach is asynchronous like your JS example.
Self signed/custom certificate
If you want to use custom certificates, you can use the sslContect
method like here:
//Load certificate
File crtFile = new File("server.crt");
Certificate certificate = CertificateFactory.getInstance("X.509").generateCertificate(new FileInputStream(crtFile));
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("server", certificate);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
//Execute the request
HttpClient client = HttpClient.newBuilder()//create a builder for the HttpClient
.sslContext(sslContext)//set the SSL context
.build();//build the HttpClient
HttpRequest request = HttpRequest.newBuilder()//create a request object, GET is the default
.uri(URI.create("url here"))//set the url
.build();//build the request object
client.sendAsync(request, BodyHandlers.ofString())//send the request in a background thread (fetch() in your code)
.thenApply(HttpResponse::body)//get the body as a string (.then(r=>r.text()) in your code))
.thenAccept(System.out::println);//print it (.then(console.log) in your code)
external library
The third possibility is to use an external library like OkHttp.