λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

λͺ©λ‘μ΄ μ—†μŠ΅λ‹ˆλ‹€.

[JAVA/μžλ°” 기초] URLConnection 클래슀

πŸ—£ Language/JAVA

    JAVAJAVA



    URLConnection Class(클래슀)



    λ„€νŠΈμ›Œν¬ 톡신에 μžˆμ–΄μ„œ λŒ€ν‘œμ μΈ ν”„λ‘œν† μ½œ 쀑 ν•˜λ‚˜κ°€ λ°”λ‘œ

    HTTP(Hyper Text Transfer Protocol)이닀.


    HTTPλž€, μ›Ήμƒμ—μ„œ 데이터λ₯Ό μ£Όκ³  받을 λ•Œ

    μ •ν•œ κ·œμ•½μ„ μ˜λ―Έν•˜λŠ”λ°, 보톡 ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„μ˜ ꡬ쑰둜 이루어진닀.


    ν΄λΌμ΄μ–ΈνŠΈμͺ½μ—λŠ” λŒ€ν‘œμ μœΌλ‘œ μ›Ή λΈŒλΌμš°μ €κ°€ 있고

    μ„œλ²„μ—λŠ” μ›Ή μ„œλ²„κ°€ μžˆλ‹€.


    그런데 λ§Œμ•½ λ‚΄κ°€ λ§Œλ“  μžλ°” ν”„λ‘œκ·Έλž¨μ—μ„œ

    μ›Ήμ„œλ²„μ—μ„œ 데이터λ₯Ό λ°›μ•„μ˜€κ³  μ‹Άλ‹€λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Όν• κΉŒ?


    이럴 λ•Œ μ‚¬μš©λ˜λŠ” ν΄λž˜μŠ€κ°€ λ°”λ‘œ

    java.net νŒ¨ν‚€μ§€ μ•„λž˜μ— μžˆλŠ” ν΄λž˜μŠ€λ“€μ΄λ‹€.



    μ΄λ ‡κ²Œ λ‹€μ–‘ν•œ ν΄λž˜μŠ€λ“€μ΄ ν™œμš©λ˜λŠ”λ°

    λŒ€ν‘œμ μœΌλ‘œ Socketκ³Ό 

    HTTP톡신에 μœ μš©ν•œ URLConnection 클래슀λ₯Ό μ†Œκ°œν•˜κ³ μž ν•œλ‹€.






    μ›λž˜λŠ” λ„€νŠΈμ›Œν¬ ν”„λ‘œκ·Έλž˜λ°μ— μžˆμ–΄μ„œ μžλ°”μ—λŠ” Socketμ΄λΌλŠ” 클래슀만이 μ‘΄μž¬ν–ˆλ‹€.

    그런데 Socket ν΄λž˜μŠ€λŠ” λ°”μ΄λ„ˆλ¦¬ ν˜•μ‹μœΌλ‘œ 데이터λ₯Ό μ „μ†‘ν•˜κ³  μˆ˜μ‹ ν•˜κΈ° λ•Œλ¬Έμ—

    ν…μŠ€νŠΈ 데이터λ₯Ό μ£Όκ³  λ°›λŠ” ν”„λ‘œκ·Έλž¨μ„ κ΅¬ν˜„ν•˜κΈ°μ—λŠ” λΆˆνŽΈν–ˆλ‹€.


    κ·Έλž˜μ„œ λ“±μž₯ν•œ ν΄λž˜μŠ€κ°€ λ°”λ‘œ

    URLConnection ν΄λž˜μŠ€μ΄λ‹€.


    URLConnection ν΄λž˜μŠ€λŠ” ν…μŠ€νŠΈλ°μ΄ν„°λ₯Ό μ£Όκ³  λ°›κ³ 

    HTTP톡신에 μ‚¬μš©λ˜λŠ” ꡬ체적인 원리λ₯Ό 잘 μ•Œμ§€ λͺ»ν•΄λ„

    URL을 ν†΅ν•΄μ„œ μ‰½κ²Œ κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜λ‹€.



    그런데 URLConnection ν΄λž˜μŠ€λŠ” 자체적으둜 μƒμ„±μžλ₯Ό ν†΅ν•΄μ„œ

    객체(μΈμŠ€ν„΄μŠ€)λ₯Ό 생성할 수 μ—†λ‹€.


    μ™œλƒν•˜λ©΄ μΆ”μƒν΄λž˜μŠ€μ΄κΈ° λ•Œλ¬Έμ΄λ‹€.



    그럼 μ–΄λ–»κ²Œ 객체λ₯Ό 생성해?


    λ°”λ‘œ URL클래슀의 λ©”μ„œλ“œλ₯Ό ν†΅ν•΄μ„œ

     URLConnection 클래슀의 객체λ₯Ό λ°›μ•„μ„œ μ‚¬μš©ν•œλ‹€.





    예제λ₯Ό ν†΅ν•΄μ„œ μ–΄λ–»κ²Œ κ΅¬ν˜„ν•˜λŠ”μ§€ μ‚΄νŽ΄λ³΄μž.


    import java.net.*;

    import java.io.*;


    public class WebSpider {

    public static void main(String[] args) {

    URL url = null;

    try {

    url = new URL("https://www.naver.com");

    } catch (MalformedURLException e) {

    System.out.println("잘λͺ»λœ URL ν˜•μ‹μž…λ‹ˆλ‹€.");

    System.exit(1);

    }

    FileOutputStream fos = null;

    try {

    URLConnection urlcon = url.openConnection();


    String contentType = urlcon.getContentType();


    System.out.println("Content Type : " + contentType);


    InputStream in = urlcon.getInputStream();

    fos = new FileOutputStream("naver.html");


    byte[] buffer = new byte[512];

    int readcount = 0;


    System.out.println("μ½μ–΄μ˜€κΈ° μ‹œμž‘ν•©λ‹ˆλ‹€.");

    while ((readcount = in.read(buffer)) != -1) {

    fos.write(buffer, 0, readcount);

    }


    System.out.println("naver.html" + " νŒŒμΌμ— λͺ¨λ‘ μ €μž₯ν–ˆμŠ΅λ‹ˆλ‹€.");

    } catch (Exception e) {

    System.out.println(e);

    } finally {

    try {

    if (fos != null)

    fos.close();

    } catch (Exception e2) {}

    }

    }

    }


    μœ„μ˜ μ˜ˆμ œλŠ” WebSpider라고 ν•΄μ„œ

    μ›Ή μ„œλ²„μͺ½μ˜ λ¬Έμ„œ 데이터λ₯Ό κΈμ–΄μ™€μ„œ

    μžμ‹ μ˜ 둜컬 μž₯μ†Œμ— htmlν™•μž₯자둜 μ €μž₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ΄λ‹€.



    URL을 λ„€μ΄λ²„λ‘œ μ§€μ •ν–ˆκΈ° λ•Œλ¬Έμ— μžμ‹ μ˜ λ‘œμ»¬μ— μ €μž₯된 νŒŒμΌμ„ μ—΄λ©΄

    λΈŒλΌμš°μ €λ₯Ό ν†΅ν•΄μ„œ λ„€μ΄λ²„μ˜ 메인 νŽ˜μ΄μ§€κ°€ λ‚˜μ˜¬ 것이닀.






    URLConnectionν΄λž˜μŠ€κ°€ νƒ„μƒν•˜κ²Œ 된 λ°°κ²½κ³Ό

    μ–΄λ–»κ²Œ ν™œμš©ν•˜μ—¬ κ΅¬ν˜„ν•˜λŠ”μ§€μ— λŒ€ν•΄μ„œ 

    μ˜ˆμ œμ™€ ν•¨κ»˜ κ°„λ‹¨ν•˜κ²Œ 정리해봀닀.