我已经开发了这段代码来获取网页的url源代码。然而,这种方法有时会失败,并且无法“抓取”页面的源代码。
private static String[] getUrlSource(String url) throws IOException {
List<String> myList = new ArrayList<String>();
URL site = new URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(
site.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
myList.add(inputLine);
in.close();
String[] arr = myList.toArray(new String[myList.size()]);
return arr;
}