java Socket 과 BufferedReader
아래는 코드는 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();
}
}
}


댓글 달기