package com.basic; import java.net.InetAddress; import java.net.UnknownHostException; import java.io.BufferedReader; import java.io.InputStreamReader; public class InetAddressTest02 { public static void main(String [] args) throws Exception{ BufferedReader reader; String url = null; InetAddress addr = null; reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("웹사이트 주소를 입력하세요 ->"); url = reader.readLine(); try{ addr = InetAddress.getByName(url); }catch(UnknownHostException e){ e.printStackTrace(); } System.out.println("=========================="); //getHostAddress(); System.out.println(url+"의 IP 번호 = "+addr.getHostAddress()); } }
package com.basic; import java.net.URL; import java.io.IOException; import java.net.MalformedURLException; public class URLEx { public static void main(String[] args) throws MalformedURLException, IOException{ URL url = new URL("http://java.sun.com/index.jsp?name=syh1011#content"); System.out.println("프로토콜 : "+url.getProtocol()); System.out.println("호스트 : "+url.getHost()); System.out.println("포트 : "+url.getDefaultPort()); System.out.println("패스 : "+url.getPath()); System.out.println("Query: "+url.getQuery()); System.out.println("reference: "+url.getRef()); System.out.println("주소: "+url.toExternalForm()); } }
package com.basic; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class NetworkEx4 { public static void main(String[] args) { URL url = null; BufferedReader input = null; String address = "http://www.google.co.kr"; String line = ""; try{ //URL : 인터넷 URL 주소를 추상화 하는 클래스 url = new URL(address); //url.openStream() : URL객체에 저장된 URL주소의 데이터를 InputStream으로 반환 input = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8")); while((line = input.readLine()) !=null) { System.out.println(line); } input.close(); }catch(Exception e){ e.printStackTrace(); } } }