创建SWT浏览器异常

时间:2017-04-06 13:10:42

标签: java swt eclipse-rcp

如何为SWT Browser的setUrl()方法创建例外?我想展示网址错误或连接问题。

1 个答案:

答案 0 :(得分:0)

您似乎只想检查给定的URL是否可用/可访问。在这种情况下,此代码将执行您想要的操作:

private static boolean isReachable(String url)
{
    try
    {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("HEAD");
        int responseCode = connection.getResponseCode();

        return responseCode == 200;
    }
    catch (IOException e)
    {
        return false;
    }
}

在致电Browser#setUrl()之前,请先致电。