프로그래밍/Java

[자바/java] IP주소 찾기 / 사이트 내용 받기

Rolen 2023. 2. 7. 16:16
package shapeTest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ShapeTest {

	public static void main(String[] args) throws IOException {
		String hostName = "www.naver.com";
		try {
			InetAddress address = InetAddress.getByName(hostName);
			InetAddress myAddress = InetAddress.getLocalHost();
			System.out.println("IP주소: " + address.getHostAddress());
			System.out.println("나의 IP주소: " + myAddress.getHostAddress());
			System.out.println(myAddress.getHostName());	// 호스트 이름 찾기
		} catch (UnknownHostException e) {
			System.out.println(hostName + "의 IP주소를 찾을 수 없습니다.");
		}
	}

}
package connectionReaderURL;

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

public class URLConnectionReader {

	public static void main(String[] args) throws Exception {
		URL site = new URL("https://www.naver.com");
		URLConnection url = site.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(url.getInputStream()));
		String inLine;
		
		while ((inLine = in.readLine()) != null) {
			System.out.println(inLine);
		}
		in.close();
	}
}
// HTML 내용을 받아서 출력한다.
728x90