2014-09-04 5 views
0

웹 사이트를 읽으려고하는데 이상하게도 일부만 반환됩니다. 그것은 단지 섹션의 중간에서 끝납니다.HttpURLConnection이 전체 내용을 읽지 않습니다.

setChunkedStreamingMode 메서드를 사용해 보았지만 아무 것도 변경하지 않았습니다.

HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection()); 
connection.setDoOutput(true); 
connection.setDoInput(true); 
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
// I write some data... 
String content = readInputStream(connection.getInputStream()); 

ReadInputStream 방법 :

private static String readInputStream(InputStream in) throws IOException { 
int len = 0; 
byte[] buffer = new byte[10000]; 
ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); 
while((len = in.read(buffer)) != -1) { 
    byteArray.write(buffer,0, len); 
} 
in.close(); 

return new String(byteArray.toByteArray()); 

}

데이터의 제한 어떤 종류가 있습니까? 에 문제가 없는지

+0

코드가 올 것 같다 아니면 올바른 길이가 있다면? –

+0

흠, 그 29118 바이트. 올바른지 확인하는 방법을 모르겠다 ... CTRL + S 및 203kB로 웹 페이지를 저장하려고했지만 그 크기를 확인하는 올바른 방법인지 여부를 모르겠다 ... – Bebras

+0

글쎄, 꽤 다른 크기. 바이트 배열에 0과 같이 문자열이 아닌 표현 가능한 바이트가 들어있는 경우 문제 중 하나가 문자열을 바이트 배열로 변환 할 수 있기 때문에 요청했습니다. –

답변

0

HttpURLConnection의 이 시도 : 당신은 당신이 새로운 문자열 (..)에 전달하는 바이트 배열 실제로 잘린 경우 (디버깅에 의해) 확인할 수

StringBuilder stringBuilder = new StringBuilder(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
String line; 
while ((line = reader.readLine()) != null) { 
     stringBuilder.append(line); 
} 
inputStream.close(); 
+0

빈 줄이없는 것 같습니다.하지만 여전히 전체 웹 페이지가 아닙니다. – Bebras

관련 문제