public boolean shutdown(int port, boolean ssl) { try { String protocol = "http" + (ssl ? "s" : ""); URL url = new URL(protocol, "127.0.0.1", port, "shutdown"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("servicemanager", "shutdown"); conn.connect(); StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); int n; char[] cbuf = new char[1024]; while ((n = br.read(cbuf, 0, cbuf.length)) != -1) sb.append(cbuf, 0, n); br.close(); String message = sb.toString().replace("<br>", "\n"); if (message.contains("Goodbye")) { cp.appendln("Shutting down the server:"); String[] lines = message.split("\n"); for (String line : lines) { cp.append("..."); cp.appendln(line); } return true; } } catch (Exception ex) { } cp.appendln("Unable to shutdown CTP"); return false; }
private boolean checkServer(int port, boolean ssl) { try { URL url = new URL("http://127.0.0.1:" + port); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int length = conn.getContentLength(); StringBuffer text = new StringBuffer(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int size = 256; char[] buf = new char[size]; int len; while ((len = isr.read(buf, 0, size)) != -1) text.append(buf, 0, len); isr.close(); if (programName.equals("ISN")) return !shutdown(port, ssl); return true; } catch (Exception ex) { return false; } }