NetUtils { public static Response request(String nginxHost, int port, String statusPath, String statusHost) { BufferedReader br = null; Socket s = new Socket(); try { s.connect(new InetSocketAddress(nginxHost, port), 100); OutputStream outputStream = s.getOutputStream(); StringBuilder requestPayload = new StringBuilder(); requestPayload.append("GET ").append(statusPath).append(" HTTP/1.1\r\n") .append("Host: ").append(statusHost).append("\r\n") .append("User-Agent: Ali-metrics Java Client\r\n") .append("Connection: close\r\n") .append("\r\n"); outputStream.write(requestPayload.toString().getBytes()); outputStream.flush(); br = new BufferedReader(new InputStreamReader(s.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; boolean start = false; boolean success = false; while ((line = br.readLine()) != null) { if (start) { sb.append(line).append("\n"); } if ("".equals(line)) { start = true; } if ("HTTP/1.1 200 OK".equals(line)) { success = true; } } String result = sb.toString().trim(); return new Response(result, success); } catch (Exception e) { return new Response(e.getMessage(), false); } finally { if (br != null) { try { br.close(); } catch (IOException e) { } } try { s.close(); } catch (IOException e) { } } }  static Response request(String nginxHost, int port, String statusPath, String statusHost);  }
@Ignore @Test public void testNetUtils() { NetUtils.Response response = NetUtils.request("127.0.0.1", 80, "/nginx_status", "127.0.0.1"); System.out.println(response.getContent()); }
@Test public void testNginxNotExist() { NetUtils.Response response = NetUtils.request("192.168.0.1", 80, "/nginx_status", "127.0.0.1"); Assert.assertFalse(response.isSuccess()); }