영어로 된 사이트의 경우 작은 크롤러를 작성하고 URL
연결을 열어 해당 작업을 수행합니다. 요청시 utf-8
으로 인코딩을 설정하고 InputStreamReader
을 요청했지만 일부 요청에 대해서는 gobbledigook을 계속 받고 다른 일부는 정상적으로 작동합니다.Java URLConnection utf-8 인코딩이 작동하지 않습니다.
다음 코드는 내가 수행 한 모든 연구와 조언을 나타냅니다. 나는 또한 URLConnection
을 HttpURLConnection
으로 바꾸려고 노력했다. 반환 된 문자열 중 일부는 다음과 같이 계속 표시됩니다.
??} r? H? P? n? c ?? d? G? o? X? {? x? "P $ (예 : defUeefee = "a", "a", "a", "b" ? 2 M ??? 3C ??
@ 내가 무엇을 놓치고
내 코드 :?.
public static String getDocumentFromUrl(String urlString) throws Exception {
String wholeDocument = null;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setConnectTimeout(60*1000); // wait only 60 seconds for a response
conn.setReadTimeout(60*1000);
InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "utf-8");
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
wholeDocument += inputLine;
}
isr.close();
in.close();
return wholeDocument;
}
샘플 URL에 오류가 있습니까? 네트워크를 통해 무엇이 발생하는지 살펴 보았습니까 (예 : Wireshark)? –
텍스트처럼 보이지 않습니다. PDF 파일일까요? 또는 압축? 나는 이것이 UTF-8의 "단순한"문제가 아닌지 의심 스럽다. –
다음은 실패한 URL입니다. 다시 말하지만, 가끔씩 만 실패합니다. 다른 사람들에게는 잘 작동합니다. http://www.broadbandtvnews.com/2014/02/04/samsung-adds-the-weather-channel/ – Eddy