ConnectException异常后面return不走了,catch中printStackTrace也不
ks_ly 2019-09-20 03:40:37 try {
URL url = new URL("http://"+getIp()+":"+getPort()); //url地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/json");
connection.connect();
os = connection.getOutputStream();
os.write(upload.getBytes("UTF-8"));
reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
String lines;
sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "UTF-8");
sbf.append(lines);
}
logger.info("下传报文为: "+sbf);
responseLine = getResult(sbf.toString());
connection.disconnect();
}catch (MalformedURLException e) {
responseLine ="创建URL出错!";
logger.error(responseLine);
e.printStackTrace();
}catch(ConnectException e){
responseLine = "创建链接失败!";
logger.error(responseLine);
e.printStackTrace();
}catch (IOException e) {
responseLine = "发送报文错误!";
logger.error(responseLine);
e.printStackTrace();
}catch (JDOMException e ){
responseLine = "解析下传报文错误!";
logger.error(responseLine);
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally {
try{
os.close();
reader.close();
sbf.setLength(0);
}catch (IOException e) {
e.printStackTrace();
}
}
logger.info("cmd HttpDatagram result."+responseLine);
return responseLine;