프로그래밍 언어/자바(JAVA)

http 프로토콜 소켓을 이용한 직접 구현(웹브라우저 구현 원리)

낙산암 2022. 5. 23. 18:00

 

 

 

public static void main(String[] args) throws Exception {

    // 소켓 및 입출력 스트림 준비
    Socket socket = new Socket("www.kocw.net", 80);
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintStream out = new PrintStream(socket.getOutputStream());

    // 요청라인
    out.println("GET / HTTP/1.1");

    // 헤더정보
    out.println("Host: www.kocw.net");
    out.println("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0)"
            + " AppleWebKit/537.36 (KHTML, like Gecko)"
            + " Chrome/30.0.1599.101 Safari/537.36");
    // 공백라인
    out.println();

    // 응답 내용
    String line = null;
    while((line = in.readLine()) != null) {
        System.out.println(line);
    }

    in.close();
    out.close();
    socket.close();

 

응답

============================================================================

 

C:\Java\jdk-11.0.2\bin\java.exe -javaagent:D:\ideaIU-2020.1.2.win\lib\idea_rt.jar=59828:D:\ideaIU-2020.1.2.win\bin -Dfile.encoding=UTF-8 -classpath C:\Users\PC-00\IdeaProjects\java_2022_05_09\out\production\java_2022_05_09 FruitAndBox2_Generic
HTTP/1.1 200 OK
Date: Mon, 23 May 2022 08:55:29 GMT
Set-Cookie: WMONID=QF8z132AFeb; Expires=Tue, 23-May-2023 17:55:29 GMT; Path=/
Content-Type: text/html; charset=utf-8
Set-Cookie: JSESSIONID=8rabZHH560OB8o7SZJu8fWPJ6gVcP0grR7bPLQcdpNa5OgNM7AThdYZ7MQYbaX8y.risswas2_servlet_engine5;Path=/;HttpOnly
Transfer-Encoding: chunked

12a


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta property="fb:admins" content="100002226971899" />
<meta http-equiv="Refresh" content="0; url=/home/index.do">
<title>KOCW</title>
</head>
<body>
</body>
</html>