我正在尝试使用 Java 获取网页的 HTML。我有这样做的代码并且它一直在工作,但是我的一些客户遇到了问题,而且它似乎只发生在 Linux 服务器上。是否有其他方法可以使用 Java for Linux 获取 HTML 或与此相关的任何问题?
protected static String getWebpageValue(String url, String contains, String endSplit) {
try {
URLConnection cnx = new URL(url).openConnection();
cnx.setRequestProperty("user-agent", "Opera/9.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(cnx.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(contains)) {
String[] couple = line.split(contains);
for (int i =0; i < couple.length ; i++) {
String[] items = couple[i].split(endSplit);
line = items[0];
}
return line;
}
}
reader.close();
} catch (ConnectException e){
e.printStackTrace();
return "URL is down";
} catch (MalformedURLException e) {
e.printStackTrace();
return "invalid URL";
} catch (IOException e) {
e.printStackTrace();
return "IOException";
}
return "Encoded String doesn't contain this";
}