/** * 測試網絡狀態 * @param transOrgan * @return */ private int testNetState(PetitionOrganTransInfo transOrgan) { int state = 200; try { //先對轉發區域網絡狀況進行檢測 String ip = transOrgan.getIpAddress(); String port = transOrgan.getPort(); String address = "http://" + ip; if (port != null && !port.equals("")) { address += ":" + port; } address += "/jubao"; URL url = new URL(address); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(10000); con.setReadTimeout(10000); state = con.getResponseCode(); } catch (Exception e) { //經過報錯來判斷網絡是否聯通 state = 0; } return state; }
經過調用上面方法判斷網絡狀態java
int state = testNetState(transOrgan); if (state != 200) { System.out.println(transOrgan.getOrgCode()+"網絡不通"); state = testNetState(transOrgan); if (state != 200) { System.out.println(transOrgan.getOrgCode()+"網絡不通"); continue; } }