java 判断webservice是否可用

来源:互联网 发布:unity3d xlua 视频 编辑:程序博客网 时间:2024/06/14 10:45

在做Java开发时,经常会用到调用webservice情况,在此,写了一个工具类,用于在调用webservice前先判断链接是否可达,代码如下:

/** * 判断URL是否可达 * @param url 待判断的URL * @return true: 可达 false: 不可达 */public static boolean urlIsReach(String url) {if (url==null) {return false;}try {HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();if (HttpURLConnection.HTTP_OK==connection.getResponseCode()) {return true;}} catch (Exception e) {return false;}return false;}


0 0