当我对一个URI进行硬编码时,它起作用,但是当我尝试遍历数组时,我得到了格式错误的URL异常。
我知道附有协议。我尝试在代码的不同位置添加和删除“”。我使用一个URI来运行它以确保其正常运行。我打印了行以查看URI是什么,以确保它们正确输出。
/**
* Convert a URI into a JSON string
*
* @param URI the URI of resource
* @return String String format of JSON object in an array
* @throws URISyntaxException
* @throws Exception
*/
private static String readUrl(ArrayList<String> urlStrings) throws Exception, IOException, URISyntaxException {
String inputLine = "";
String finalInput = "";
int i;
for(i = 0;i<urlStrings.size();) {
System.out.println(urlStrings.size());
System.out.println(urlStrings.get(i));
if (urlStrings.get(i).contains("WADL") || urlStrings.get(i).contains("wadl") || urlStrings.get(i).contains("WSDL") || urlStrings.get(i).contains("wsdl")) {
return null;
}
// 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(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println("Error" + e);
}
BufferedReader in = null;
String uri = urlStrings.get(i);
URL url = new URL(uri);
try {
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
//create one JSON object of all endpoints
finalInput += inputLine;
}
System.out.println("Successfully processed: " + uri);
System.out.println(finalInput);
i++;
return finalInput + "\n";
}
catch(IOException e) {
System.out.println("Cannot read: " + uri);
return null;
}
finally {
if (in != null)
in.close();
}
}
i++;
//return JSON object to service catalog
return finalInput;
}
希望我可以遍历数组,调用所有端点,并构建一个JSON对象以返回前端。