测试网关及http是否连接的函数

来源:互联网 发布:php版本 编辑:程序博客网 时间:2024/06/04 00:38

测试当前的网关是否连接上

/**

       * Ping current network'sgate way. Return the result of ping operation.

       *

       * @return Return trueif ping operation is success,false otherwise.

       */

      publicboolean ConennectTest(){

            StringgateWay = getGateWay();

            if(gateWay ==null) {

                  returnfalse;

            }

           

            try {

                  Socketserver = new Socket();

                  InetSocketAddressaddress = new InetSocketAddress(gateWay, 80);

                  server.connect(address,5000);

                  server.close();

                  returntrue;

            }catch(UnknownHostException e) {

                  returnfalse;

            }catch (IOException e){

                  returnfalse;

            }

      }

测试是否连接上http

/**

       * Use httpconnect to Google, test if the network connected or not.

       *

       * @return Return truestands for network connect to Internet, false otherwise.

       */

      publicstaticbooleanhttpConnectTest() {

             if(EthernetImplement.dummyMode){

                   returntrue;

             }

             

            try{

                  url =new URL("http://www.google.com");

                  connect =(HttpURLConnection)url.openConnection();

                  connect.setRequestMethod("GET");

                  connect.setConnectTimeout(6*1000);

                  code =connect.getResponseCode();

            }catch(Exception e) {

                  returnfalse;

            }

           

            if(code ==HttpURLConnection.HTTP_OK)

            {

                  returntrue;

            }

            else {

                  returnfalse;

            }

      }

原创粉丝点击