public static String loadJson (String url) {
String username = "*****";
String password = "******";
StringBuilder json = new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
String encoding = new String(Base64.encode(new
String(username+":"+password).getBytes()));
((HttpURLConnection) uc).setRequestMethod("GET");
uc.setRequestProperty("Content-Type", "text/plain");
uc.setRequestProperty("charset", "UTF-8");
uc.setRequestProperty( "Authorization","Basic "+encoding);
uc.connect();
BufferedReader in = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json.toString();
}
//跳过SSL证书检测
private static void disableSslVerification() {
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
{
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
disableSslVerification();
String url1 = "https://10.0.19.195/redfish/v1/Chassis/1/NetworkAdapters/PCIeSlot1/NetworkPorts/1";
String json = loadJson(url1);
JSONObject jsonObject = JSONObject.fromObject(json);
System.out.println(jsonObject.get("AssociatedNetworkAddresses"));
}
注:如果url是https开头的,跳过SSL证书检测