网络:检查请求地址是否存在

    /**
     * 检查请求地址是否存在
     *@param @param  URLName
     *@param @return
     *@return boolean
     *@throws
     */
    public boolean UrlExists(String URLName) {

  try {

   // 设置此类是否应该自动执行 HTTP 重定向(响应代码为 3xx 的请求)。
   HttpURLConnection.setFollowRedirects(false);
   URL url = new URL(URLName);
   HttpURLConnection con = (HttpURLConnection) url.openConnection();

   // 设置 URL 请求的方法, GET POST HEAD OPTIONS PUT DELETE TRACE
   con.setRequestMethod("HEAD");

   // 从 HTTP 响应消息获取状态码
   return (con.getResponseCode() == HttpURLConnection.HTTP_OK);

  } catch (Exception e) {

   e.printStackTrace();

   return false;

  }

 }

    public static void main(String[] args) {  
        try {  
            HttpRequester request = new HttpRequester();  
//            HttpRespons hr = request.sendGet("http://www.csdn.net");  
//   
//            System.out.println(hr.getUrlString());  
//            System.out.println(hr.getProtocol());  
//            System.out.println(hr.getHost());  
//            System.out.println(hr.getPort());  
//            System.out.println(hr.getContentEncoding());  
//            System.out.println(hr.getMethod());  
//              
//            System.out.println(hr.getContent());  
            String URLName = "http://ecol.ilutu.com/ecol/publisher/authAction!wAsynUpdate?random=5826";
            boolean is = request.UrlExists(URLName);
            System.out.println(is);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }

你可能感兴趣的:(网络)