자바

 

 

 

 

 

 

class Host2ip

package java18;

import java.net.InetAddress;

public class Host2ip {

	public static void main(String[] args) {
		String hostname="www.google.com";
		try {
			//도메인을 Ip 주소로 변환
/*			InetAddress address=InetAddress.getByName(hostname);
			System.out.println("IP 주소 : " + address.getHostAddress());	
			*/
//하나의 도메인에 복수의 IP 주소가 있는 경우			
			InetAddress[] addresses=InetAddress.getAllByName(hostname);
			for(InetAddress adress: addresses){
				System.out.println("IP 주소 : " + adress.getHostAddress());
			}
			
		} catch (Exception e) {
			System.out.println("IP 주소를 찾을 수 없습니다.");
			e.printStackTrace();
		}
	}
	
	
	
	
}




 

IP 주소 : 74.125.203.99
IP 주소 : 74.125.203.147
IP 주소 : 74.125.203.104
IP 주소 : 74.125.203.103
IP 주소 : 74.125.203.105
IP 주소 : 74.125.203.106

 

 

class URLConnectionReader

 

package java18;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

//URL 클래스 : url 정보를 분석
//URLConnection 클래스 : url에 접속
public class URLConnectionReader {

	public static void main(String[] args) {
		try {
			URL site =new URL("https://www.google.co.kr/");//url 객체 생성
			URLConnection url=site.openConnection();//url에 접속
			BufferedReader in=new BufferedReader
					(new InputStreamReader(url.getInputStream() ));//url의 내용을 읽는 스트림
			
			String str;
			while((str=in.readLine())!=null){
				System.out.println(str);
			}
			
			in.close();//스트림 닫기
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	
	
}

 

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ko"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script>(function(){window.google={kEI:'e-sFWbHrEoO00ATujav4DA',kEXPI:'1352553,1352864,1352993,1353095,3700253,3700347,4026241,4029815,4031109,4032678,4036527,4039268,4040138,4043492,4045839,4048347,4064904,4065786,4072364,4072776,4076095,4076999,4078430,4081038,4081164,4082165,4083458,4084179,4090550,4090553,4090806,4090877,4091353,4092934,4093313,4094251,4094544,4094837,4095910,4095999,4096323,4097153,4097922,4097929,4098096,4098721,4098728,4098752,4100169,4100379,4100828,4101376,4101429,4101750,4102975,4103214,4103470,4103473,4103845,4103849,4104204,4104723,4105085,4105178,4105240,4105317,4105469,4105563,4105811,4106176,4106606,4106949,4107221,4107424,4107555,4107628,4107966,4107989,4108505,4108538,4108540,4109075,4109316,4109498,4109630,8503585,8508229,8508931,8509037,8509373,8509826,10200083,10200095,10202285,19001731,19002021,41027342',authuser:0,kscs:'c9c918f0_24'};google.kHL='ko';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function()

....

 

 

 

class HttpURLTest

package java18;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
//URLConnection - 다양한 프로토콜에 대한 url 접속
// HttpURLConnection  - http 전용 접속
//url?q=java
//쿼리스트링(query string, 질의문자열)
//변수명 =값
public class HttpURLTest {

	public static void main(String[] args) throws Exception{
		HttpURLTest http=new HttpURLTest();
		String site="http://google.com/search?q=java";
		URL url =new URL(site); //url 객체 생성
		//http url 에 접속
		HttpURLConnection conn =(HttpURLConnection)url.openConnection();
		//접속 방식(get, post)
		conn.setRequestMethod("GET");
		
		//웹서버에 대한 요청은 header 와 body 로 구성됨
		conn.setRequestProperty("User-Agent", "Mozilla/5.0");//브라우저 정보
		int resCode=conn.getResponseCode();//서버의 응답 코드
		System.out.println("응답코드 : " + resCode);

//200 success(정상처리) ,  404 잘못된 주소, 500 서버측 에러
		BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String inputLine;
		StringBuffer output=new StringBuffer();
		while((inputLine=in.readLine())!=null){
			output.append(inputLine);
		}
		
		System.out.println(output.toString());
		
	}
	
	
}

 

 

응답코드 : 200
<!doctype html><html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="ko"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><link href="/images/branding/product/ico/googleg_lodp.ico" rel="shortcut icon"><noscript><meta 

..

 

 

 

 

 

 

 

java

 

about author

PHRASE

Level 60  머나먼나라

한 작가가 아직 살아 있을 때는 우리는 그의 가장 못한 작품으로 그를 평가하고, 그가 죽으면 그의 가장 뛰어난 작품으로 그를 평가한다. -새뮤얼 존슨

댓글 ( 4)

댓글 남기기

작성