JimmyÀÇ È¨ÆäÀÌÁö > Java > get 2. remoteÀÇ htmlÁ¤º¸ °¡Á®¿À±â

       

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.StringTokenizer;
public class GetHttp {
 Socket smtpSocket = null;
 BufferedReader  smtpIn  = null;
 PrintWriter smtpOut = null;

 public String getData(String host, String url, String port, 
                        String startTag, String endTag){
   String msg= null;
   String getMsg = null;
   boolean isCorrect = true;
   boolean isFirst = true;
   StringBuffer selectedData = new StringBuffer();

   try {
     smtpSocket = new Socket(host, Integer.valueOf(port).intValue() );
     smtpIn     = new BufferedReader(new InputStreamReader(
                      smtpSocket.getInputStream()));
     smtpOut    = new PrintWriter(smtpSocket.getOutputStream(),true);
   } catch (UnknownHostException e) {
     System.err.println("Don't know about host: " + host);
     System.exit(1);
   } catch (IOException e) {
      System.err.println("Couldn't get I/O for "
            + "the connection to: taranis.");
                      System.exit(1);
   }
   getMsg = "GET " + url + " HTTP/1.0";
    smtpOut.println(getMsg);
   smtpOut.println("");
   smtpOut.flush();

   //header °¡Á®¿À±â
   while(isCorrect) {
    try {
     msg = smtpIn.readLine() ;
     System.out.println(msg);
     if(isFirst){
      isFirst = false;
      if( msg.startsWith("HTTP/1.0 2") || msg.startsWith("HTTP/1.1 2") ){
      }else{
        isCorrect = false;
        break;
      }
     }
     if(msg.length() == 0 )
      break;
    } catch(IOException e) {
     System.err.println("Couldn't get I/O for ");
     System.exit(1);
    } finally{
    }
   }

//startTagÀüÀÇ data °¡Á®¿À±â
   while(isCorrect) {
    try {
     msg = smtpIn.readLine() ;
     if(msg == null){
       isCorrect = false;
       break;
     }
     if(msg.startsWith(startTag)){
      break;
     }
     //System.out.println(msg);
    } catch(IOException e) {
     System.err.println("Couldn't get I/O for ");
     System.exit(1);
    } finally{
    }
   }

//data °¡Á®¿À±â
   boolean isEndTag = false;
   while(isCorrect) {
    try {
     msg = smtpIn.readLine() ;
     if(msg == null){
       break;
     }
     if(msg.endsWith(endTag)){
      isEndTag = true;
      break;
     }else{
      selectedData.append(msg);
      selectedData.append("\n");
     }
     System.out.println(msg);
    } catch(IOException e) {
     System.err.println("Couldn't get I/O for ");
     System.exit(1);
    } finally{
    }
   }
   if(!isEndTag)
    selectedData = new StringBuffer();


  //½Ã½ºÅÛ Á¾·á
   try{
     smtpIn.close() ;
     smtpSocket.close();
   } catch(IOException e) {
     System.err.println("Couldn't get I/O for ");
     System.exit(1);
   }
   if(isCorrect)
     System.err.println("\r\n Good !!!");
   else
     System.err.println("\r\n Bad !!!");
   return selectedData.toString();

 }

}

¼³¸í:
 getData(String host, String url, String port, 
         String startTag, String endTag)
 ÁöÁ¤µÈ host¿¡¼­ ÁöÁ¤µÈ url³»¿ëÁß ±¸ºÐÀÚ·Î ºÐ¸®µÈ ³»¿ëÀ» °¡Á®¿À´Â
 methodÀÔ´Ï´Ù.        



À§ÀÇ ¼Ò½º¿¡ 
±Ã±ÝÇÑÁ¡ÀÖÀ¸¸é Q&A¿¡ ±ÛÀ»³²°ÜÁÖ¼¼¿ä       

       
       

Jimmy´Â ´©±¸Àΰ¡bit10ÁÖ¼Ò·Ï - ¹æ¸í·Ï - Q&A - E-mail

½Åºñ·Î - °æÈñ´ëÀüÀÚ°ú88Çйø¸ðÀÓ¹æ - ¶óÀÌÄÚ½º - Æ®¶óÀÌÆ÷µå


Âü°í·Î Á¦È¨ÆäÀÌÁöÀÇ Çü½ÄÀº ¶óÀÌÄÚ½º ȨÆäÀÌÁöÀÇ ¼Ò½º¸¦ ÂüÁ¶ÇÏ¿´½À´Ï´Ù.