Страницы

четверг, 1 декабря 2011 г.

Проверка доступности сайта/хоста

boolean isHostReachable(String address) {
    try {
        URL url = new URL(address);

        HttpURLConnection urlc = 
            (HttpURLConnection) url.openConnection();
        urlc.setRequestProperty("User-Agent", "userAgent");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1000 * 10);
        urlc.connect();
        if (urlc.getResponseCode() == 200) {
            urlc.disconnect();
            return true;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

В коде ничего сложного нет: создаём подключение к интересующему хосту, задаём user agent, таймайт в миллисекундах, подключаемся и проверяем код ответа, после чего тут же отключаемся.

1 комментарий:

  1. Анонимный16 мая 2013 г., 8:37

    Hi,

     I am a  student and Mac OS X is new for me. I got an assignment . My aim is to hook MAC OS X at boot time after this i want to run my application to do some operation on boot volume. Then boot volume must be mount.

    Basically when i run my application then system must be reboot. After this reboot and before mounting of boot volume i want to do some operation on that boot volume . Then boot volume must be mount after that reboot.

    For this i am trying to boot my system from ram with the help of that boot volume on which i want to do some operation.

    For this i m using hdik , hdid , hdiutil command .


     Basically i want to mount my boot volume into RAM at reboot time.

    If you know any method , please guide me.

    Kindly help me.

    ОтветитьУдалить