我有一个类似 this 的 URL以及以下方法
public static void saveContent( String webURL )throws Exception
{
URL website = new URL(webURL);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
System.out.println(response.toString());
}
但是,当我想打印网页内容时,它总是获取主页(www.google.com)的源代码。
我该如何解决我的问题?感谢您的帮助。
最佳答案
我将您的代码复制到 netbeans,它似乎工作正常。我认为问题可能会导致方法参数“webURL”中的内容。尝试在 Debug模式下运行您的应用程序,然后看看您得到了什么。
关于java - 使用java获取网页源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345719/