2013년 11월 26일 화요일

SimpleURLCheck

 import java.net.*;
    import java.io.*;

    public class SimpleURLCheck {

      public static void main(String args[]) {
        if (args.length == 0) {
          System.err.println(
              "Please provide a URL to check");
        } else {
          HttpURLConnection.setFollowRedirects(false);
          String urlString = args[0];
          try {
            URL url = new URL(urlString);
            URLConnection connection =
              url.openConnection();
            if (connection instanceof HttpURLConnection) {
              HttpURLConnection httpConnection =
               (HttpURLConnection)connection;
              httpConnection.connect();
              int response =
               httpConnection.getResponseCode();
              System.out.println("Response: " + response);
              String location =         
               httpConnection.getHeaderField("Location");
              if (location != null) {
                System.out.println(
                 "Location: " + location);
              }
              InputStream is =
                httpConnection.getInputStream();
              byte[] buffer = new byte [256];
              while (is.read (buffer) != -1) {}
              is.close();
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }

댓글 없음:

댓글 쓰기