java Socket 과 BufferedReader

thepath의 이미지

아래는 코드는 socket 을 사용해서 웹페이지를 요청한 결과를 한 줄 씩 읽어서 처리하는 메소드입니다.

그런데, 가끔씩 한 줄이 완전히 읽혀지지 않을 때가 있습니다.

한 줄을 완전히 읽을 방법이 없을 까요?

예) 다음 커뮤니티의 한메일서비스에서 '편지 읽기' 페이지 요청시

Action a = new Action() {
  public void run(Object[] params) {
      String aLine = (String) params[0];
      if (-1 < aLine.indexOf("원문받기")) {
        System.out.println(aLine);
      }
      ...
  }
}

String req = "/Mail-bin/view_mail?" +
"MSGID=????????????????" +
"&FOLDER=??????" +
"&mpage=2&KEYTYPE=&KEYWORD=&US=6scp" +
"&_top_hm=li_normalread";
request(req,"wwl224.daum.net",a,false);

결과

웹페이지에는 '원문받기' 버튼이 두 개 가 있는데

첫번째 '원문받기' 버튼 부분의 태그가 완전히 안 읽혔습니다.

웹브라우저에서 '소소보기'로 보았을 때는 첫번째 두번째 '원문받기' 버튼에 대한 태그가 한 줄에 써있었습니다.

<input type=button value='원문받기' onclick='location="http://wwl224.hanmail.net/Mail-bin/view_submsg.cgi?TM=???????????????????????????????????????????????????????????Bsmp

<input type=button value='원문받기' onclick='location="http://wwl224.hanmail.net/Mail-bin/view_submsg.cgi?TM=???????????????????????????????????????????????????????????Bsmp???????????????????????????...?????????????"' target=right class=btn>

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

interface Action {
  public void run(Object[] params);
}

.....
public class Daum {

...

  public void request(String req,String host,Action act,boolean print) {
    try {
      Socket socket = new Socket(host,80);
      BufferedReader br
       = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
      StringBuffer head = new StringBuffer();
      head.append("GET ");
      head.append(req);
      head.append(" HTTP/1.1\r\n");
      head.append("Host: ");
      head.append(host);
      head.append("\r\n");
      head.append("Cookie: ");
      head.append(cookie.toString());
      head.append("\r\n\r\n");
      dos.writeBytes(head.toString());
      dos.flush();
      if (print) {
        System.out.println("print head\n"+head);
      }
      int len;
      String aLine;
      Object[] params = new Object[2];
      if (print) {
        while (null != (aLine=br.readLine())) {
          System.out.println(aLine);
          params[0] = new String(aLine);
          params[1] = this;
          act.run(params);
        }
      } else {
        while (null != (aLine=br.readLine())) {
          params[0] = new String(aLine);
          params[1] = this;
          act.run(params);
        }
      }
      dos.close();
      br.close();
      socket.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.