At first, you can use String.format
for the formatting:
String url=String.format("https://%s/redfish/v1/%s",ip,query);
You could also use MessageFormat
if you want to.
For connecting, you can create a URL
-object and creating a URLConnection
(in your case HttpsURLConnection
) and opening an InputStream
for the response afterwards:
HttpsURLConnectioncon=(HttpsURLConnection)new URL(url).openConnection();
try(BufferedInputStream is=new BufferedInputStream(con.openStream()){
//...
}
In order to do the authentication, you can take a look at this tutorial:
String auth = "ADMIN:ADMIN";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
//Get the HttpURLConnection
con.setRequestProperty("Authorization", authHeaderValue);
//Connect/open InputStream
If you really want to disable verification, you can create your own HostnameVerifier
that allows everything but this is strongly discouraged as this allows man in the middle attacks as you basically disable the security of HTTPs:
con.setHostnameVerifier((hostname,sslSession)->true);
All combined, it could look like this:
String url=String.format("https://%s/redfish/v1/%s",ip,query);
String auth = "ADMIN:ADMIN";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
HttpsURLConnection con=(HttpsURLConnection)new URL(url).openConnection();
con.setRequestProperty("Authorization", authHeaderValue);
con.setHostnameVerifier((hostname,sslSession)->true);//vulnerable to man in the middle attacks
try(BufferedInputStream is=new BufferedInputStream(con.openStream()){
//...
}