Opens a URL and returns the contents as a string. Returns " " if there an error, including host or file not found.
(String url)
| 2119 | Returns "<Error: message>" if there an error, including |
| 2120 | host or file not found. */ |
| 2121 | public static String openUrlAsString(String url) { |
| 2122 | //if (!trustManagerCreated && url.contains("nih.gov")) trustAllCerts(); |
| 2123 | url = Opener.updateUrl(url); |
| 2124 | if (debugMode) log("OpenUrlAsString: "+url); |
| 2125 | StringBuffer sb = null; |
| 2126 | url = url.replaceAll(" ", "%20"); |
| 2127 | try { |
| 2128 | //if (url.contains("nih.gov")) addRootCA(); |
| 2129 | URL u = new URL(url); |
| 2130 | URLConnection uc = u.openConnection(); |
| 2131 | long len = uc.getContentLength(); |
| 2132 | if (len>5242880L) |
| 2133 | return "<Error: file is larger than 5MB>"; |
| 2134 | InputStream in = u.openStream(); |
| 2135 | BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8")); |
| 2136 | sb = new StringBuffer() ; |
| 2137 | String line; |
| 2138 | while ((line=br.readLine()) != null) |
| 2139 | sb.append (line + "\n"); |
| 2140 | in.close (); |
| 2141 | } catch (Exception e) { |
| 2142 | return("<Error: "+e+">"); |
| 2143 | } |
| 2144 | if (sb!=null) |
| 2145 | return new String(sb); |
| 2146 | else |
| 2147 | return ""; |
| 2148 | } |
| 2149 | |
| 2150 | /** Saves the current image, lookup table, selection or text window to the specified file path. |
| 2151 | The path must end in ".tif", ".jpg", ".gif", ".zip", ".raw", ".avi", ".bmp", ".fits", ".pgm", ".png", ".lut", ".roi" or ".txt". */ |
no test coverage detected