URL にアクセスしてそこからストリームを読み取るコードをいくつか開発しましたが、ストリームを取得しようとすると java.net.SocketException が発生します。スタック トレースは次のとおりです。
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:652)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at utils.AljazemArabicWordsGrabber.grab(AljazemArabicWordsGrabber.java:46)
at utils.TranslatorThread.run(TranslatorThread.java:39)
例外を引き起こすコードは次のとおりです。
public String[] grab(EnglishWord englishWord) {
try {
aljazemURL = new URL(urlLink + englishWord.getLemma());
connection = aljazemURL.openConnection();
//connection.connect();
// System.out.println("connection:" + connection);
//// stream = connection.getInputStream();
// reader = new InputStreamReader(stream);
in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // the exception occurs here..
while ((decodedString = in.readLine()) != null) {
if (decodedString.contains("<div class=\"default_to_trans_ar\" style=\"display:block\">")) {
decodedString = decodedString.replace("<div class=\"default_to_trans_ar\" style=\"display:block\">", "");
decodedString = decodedString.replace("</div>", "");
slicedWords = decodedString.split(",");
for (String slice : slicedWords) {
System.out.println(slice);
}
// System.out.println(decodedString);
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return slicedWords;
}