我开发了一些可以访问 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;
}