선택장애

자바(JAVA) - url, html보기 본문

자바(JAVA)

자바(JAVA) - url, html보기

yes or yes 2017. 8. 14. 14:10
반응형


package Network;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class URLEx {
public static void main(String[] args) throws MalformedURLException, IOException{
   URL url = new URL("http", "naver.com", 80, "");   //가져올 URL
   String protocol=url.getProtocol();
   String host= url.getHost();
   int port = url.getPort();
   int defaultPort=url.getDefaultPort();
   
   
   String path = url.getPath();
   String query = url.getQuery();   //뒤에 검색되는 내용을 적어준다
   String ref=url.getRef();     //뒤에 검색되는 내용을 적어준다
   String _url=url.toExternalForm();
   String mixUrl = null;
   if(port==-1){
      mixUrl=protocol+"//"+host+path+"?"+query+"#"+ref;    //실제 url에 적히는 내용
   }else{
      mixUrl=protocol+"//"+host+":"+port+path+"?"+query+"#"+ref; //실제 url에 적히는 내용
   }


   if(port==-1)
      port=url.getDefaultPort();
   System.out.printf("프로토콜 : %s %n", protocol);    //출력부분
   System.out.printf("호스트: %s %n", host);
   System.out.printf("포트 : %d %n", port);
   System.out.printf("path : %s %n", path);
   System.out.printf("쿼리 : %s %n", query);
   System.out.printf("ref : %s %n", ref);
   System.out.printf("mixURL : %s %n", mixUrl);
   System.out.printf("URL : %s %n", _url);

 

url=new URL(http://java.sun.com");   //가져올 홈페이지URL
   InputStream input = url.openStream();
   int readByte;
System.out.println("===문서의 내용===");
   while(((readByte=input.read())!=-1)){  //홈페이지의 html을 가져온다.
      System.out.print((char) readByte);
   }
   
   
}
}
반응형