Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


HttpURLConnection »ç¿ë¿¹¿Í °³³ä
 
ÀÚ¹ÙÆÐÅÏ ½ºÅ͵ðÁß¿¡ ³ª¿Ô´ø URLConnectionÀÇ °³³ä¿¡ ´ëÇÑ °£·«ÇÑ ¿ä¾àÀ» ÇÏ¿© ¾ÆƼŬÀ» ¸¸µé¾ú½À´Ï´Ù. ( 2004/01/15 ) 165
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 


ÇöÀç URLConnection¸¸À» ÀÌ¿ëÇؼ­ ÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó Àüü ¾ÖÇø®ÄÉÀ̼ÇÀÇ ÀϺÎÁßÀÇ

³»¿ëÀ» URLConnectionÀ» ÀÌ¿ëÇÏ¿© À¥À» Çڵ鸵ÇÏ·Á ÇÏ°í ÀÖÀ»°Ì´Ï´Ù.

¿ì¼± °£´ÜÇÏ°Ô ¸»¾¸µå¸®¸é ÀϹÝÀûÀÎ socketÀ» ÀÌ¿ëÇÏ¿© ½ÇÁ¦ 󸮵µ °¡´ÉÇϳª

JDK¿¡¼­ Ãß»óÈ­½ÃÄÑ http ¹× ftpÇÁ·ÎÅäÄÝÀÇ Æ¯¼ºÀ» »ì·Á³½°ÍÀ̶óÇÒ¼ö ÀÖ½À´Ï´Ù.

´ç¿¬È÷ http¸¸À» ¿¹·Î µé¾îº¸¸é statelessÇÏ´Ù¶ó´Â °Ç ¾Ë°í °è½Ã°ÚÁÒ?

 
Á¦°¡ ¿¹Àü¿¡ ÀÛ¼ºÇÑ°Ô ÀÖ¾ú´Âµ¥ ³ë¶ÑºÏ ¾îµð¿¡ ÀÖ´ÂÁö¸¦ ¸ð¸£°Ú³×¿ä...

°£´ÜÇÏ°Ô ³ª¸¶ »ùÇÃÇϳª º¸¿©µå¸®¸ç ÀÌ°ÍÀ¸·Î ¹«¾ùÀ» Çß¾ú´ÂÁö¸¦ ¼³¸íÇϵµ·Ï ÇÏÁÒ.. ^^

Á˼ÛÇѵ¥ Áö±Ý ±ÞÇÏ°Ô ¸¸µå´À¶ó ÁÖ¼®ÀÌ ¾ø½À´Ï´Ù..¾çÇØÇØÁÖ¼¼¿ä..

 Source : HttpURLConnection.java



import java.net.*;
import java.io.*;

public class  HttpURLConnectionTest extends HttpURLConnection
{
 static URL url;
 static URLConnection conn;
 static
 {
  try 
  {
   url = new URL("http","java.sun.com", 80,"/"); 
   conn = url.openConnection();
  }catch(Exception e) 
  {

  }
 }
 public HttpURLConnectionTest() 
 {
  super(url);  
  go();
 }
 public void go() 
 {
  try{
   conn.connect();
   System.out.println("++++++++Start+++++++++++");
   System.out.println("getContentType()  : " + conn.getContentType() );
   System.out.println("getContentLength() : " + conn.getContentLength());
   System.out.println("getUseCaches() : " + conn.getUseCaches());
   System.out.println("getContent() : " + conn.getContent());
   System.out.println("getRequestMethod() : " + ((HttpURLConnection)conn).getRequestMethod());
   System.out.println("getResponseCode() : " + ((HttpURLConnection)conn).getResponseCode());
   System.out.println("getResponseMessage() : " + ((HttpURLConnection)conn).getResponseMessage());
   System.out.println(conn);
   System.out.println("++++++++++++++++++++++");
   InputStream is = conn.getInputStream();
   byte [] b = new byte[1024];
   int len = 0;
   while( (len = is.read(b, 0, b.length)) != -1)
   {
    System.out.write(b, 0, len);
   }
  }catch(Exception e) {
   e.printStackTrace();
  }
 }
 public void disconnect(){}
 public boolean usingProxy(){ return false;}
 public void connect() {
  
 }
 public static void main(String[] args) 
 {
  new HttpURLConnectionTest();
 }
}

À§ÀÇ Å¬·¡½º¸¦ ÀÌ¿ëÇÏ¿© ½ÇÇà½ÃÄÑ º¸¸é ÇÁ·ÎÅäÄÝÀÇ ½ÇÁ¦ ¿äû°ú ´ä¿¡ ´ëÇÑ °ÍÀÌ ³ª¿À°Ô µÇ´Âµ¥ ±×·¸°Ô ÇϽ÷Á¸é rfcÆ÷¸Ë http¹®¼­¸¦ Àо¼Å¾ß ÇÕ´Ï´Ù. ÇÏÁö¸¸ ³Ê¹« ¸¹±â¿¡ ±×³É http request¿Í response¿¡ ´ëÇÑ Á¤º¸¸¸À» ¾Ë·Áµå¸®¸é request½Ã¿¡ <method><resource identifier><HTTP version><crlf> [<Header>: <value>] <crlf> ......... [<Header>: <value>] <crlf> <crlf> [Entity body] ¿¹Á¦: GET /index.html HTTP/1.1 Accept: text/html Accept: audio/x User-agent: Carouser ·Î Ç¥ÇöµÇ¸ç ÀÏ¹Ý console¿¡¼­ Å×½ºÆ®½Ã¿¡ telnet xx.xx.com 80 À̶ó°í Ä¡½ÅÈÄ À§ÀÇ GET /index.html HTTP/1.1 ¹®ÀÚ¸¦ ÀÔ·ÂÇÑÈÄ ¿£Å͸¦ Ä¡°Ô µÇ¸é ÇØ´ç »çÀÌÆ®ÀÇ root documentÁ¤º¸¸¦ ¾ò°Ô µÇ¸ç ¹Þ´Â response·Î´Â <HTTP> <result code> [<explanation>]<crlf> [<Header>: <value>]<crlf> ...... [<Header>: <value>]<crlf> <crlf> [Entity body] ¿¹Á¦: HTTP 200 Document follows Date: Sat, 07 Mar 1998 09:59:15 GMT Server: NCSA/1.5.2 Last-modified: Sun, 07 Dec 1997 12:09:04 GMT Content-type: text/html Content-length: 388 ÀÇ Á¤º¸¸¦ ¾ò°Ô µË´Ï´Ù. ±âº»ÀûÀ¸·Î ÀÌ·¯ÇÑ °ÍÀ» javaÃøÀ¸·Î ¿Å°ÜÁÙ¼ö ÀÖ´Â °ÍÀÌ urlconnectionÀε¥ ÀÌ°ÍÀ» ÀÌ¿ëÇÏ¿© ¿¹Àü¿¡ ¹Ì´Ï °Ë»ö¿£ÁøÀ» ¸¸µé¾î³Â½À´Ï´Ù. ·Îº¿°úµµ °°Àº °³³äÀÌÁö¿ä... °Ë»ö¿£Áø¿¡ µî·ÏÇϼź¸¼ÌÁÒ? °Å±â¿¡ µî·ÏÇÏ¸é »çÀÌÆ®¿¡ ·Î±×ÀÎÀÌ ÇÊ¿äÇϸé Å×½ºÆ®¿ë id, pass¸¦ ÀÔ·ÂÇϵµ·Ï Çϴµ¥ Á÷¿øÀ» ±×°÷¿¡ µé¾î°¡¼­ content¸¦ ã°Ô ÇÒ¼öµµ Àִ°Űí Àß ¸¸µé¾îÁ³´Ù¸é ·Îº¿À» ½ÃÄÑ µ¹¾Æ´Ù´Ï°Ô ÇÒ¼öµµ ÀÖ°ÚÁÒ? ¿¹Àü¿¡ webzip°°Àº ÅøÀ» ½áº¸¼Ì³ª¿ä? »çÀÌÆ®¸¦ Åë°·Î ±Ü¾î´ÙÁÖ´Â ÅøÀÌÁÒ? ¸»ÀÌ Á¶±Ý ´Ù¸¥°÷À¸·Î »õ±äÇߴµ¥ ±×·¡¼­ Á¦°¡ ¸¸µé¾ú´ø°ÍÀº ÇÁ·ÎÁ§Æ®¶§ ¸¸µé¾ú´ø »çÀÌÆ®ÀÇ Á¤º¸¸¦ database¿¡¼­ °Ë»öÀ» Çϱ⿡´Â content¸¦ ¼öÁýÇϱ⿡ ¸ðÀÚ¶õ Áö¶ó °¡»ó ·Îº¿À» Çϳª ¸¸µé¾úÁö¿ä... °Â°¡ ¹¹¸¦ ÇÏ³Ä¸é »õº®3½Ã¸¸ µÇ¸é ±ú¾î³ª¼­, »çÀÌÆ®¸¦ µÚÁö±â ½ÃÀÛÇÕ´Ï´Ù. À§ÀÇ Å¬·¡½ºÀÇ ÆÄÀÏ÷ºÎÇϴϱî Çѹø ½ÇÇàÇغ¸¼¼¿ä... 200 OK»çÀÎÀ» ¹ÞÀº ÈÄ content¸¦ html·Î ´Ù ¾ò¾î³»°Ô µÇ°Åµç¿ä? ±×·¸°Ô ¾ò¾î³½ content¸¦ ÆÐÅÏÀ» ÅëÇØ ºÐ¼®À» ÇÏ°Ô µÇÁö¿ä.. ^^ ±× ºÐ¼®À» ÅëÇÏ¿© anchor tag°¡ ºÙ¿©Áø ¾ÖµéÀ» ã°Ô µË´Ï´Ù. (±× anchor tag¸¦ recursiveÇÏ°Ô µ¹¸®¸é ¾î¶»°Ô µÉ±î¿ä?¤¾¤¾ ±× »çÀÌÆ®¸¦ Åë°·Î µÚÁú¼ö ÀÖ´Ù´Â °Å³×¿ä?.. ^^) ±×¸®°í ÆäÀÌÁö¿¡ ºÙ¾îÀÖ´Â ¸ÞŸ ű׸¦ ¾ò¾î³»°í ±×°ÍÀ» ºÐ¼®ÇÏ¿© Çٽɳ»¿ëÀ» database¿¡ ÀúÀåÇÏ°Ô µË´Ï´Ù. ±×·¸°Ô ÇÏ°í ³­ÈÄ »ç¿ëÀÚÀÇ °Ë»öÀÌ ÀÖÀ¸¸é ±× ÆäÀÌÁöÀÇ url°ú Á¤º¸¸¦ »ç¿ëÀÚ¿¡°Ô º¸¿©ÁÖ°Ô µÇ´Â°ÍÀÌÁÒ.. °£´ÜÇÏ°Ô URLConnection¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸¾Ò½À´Ï´Ù.
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD