我们如何使用 JAVA 下载 HTML 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3341516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:23:20 来源:igfitidea点击:
How we can Download a HTML Page using JAVA?
提问by Klark
How we can Download a HTML Page using JAVA??
我们如何使用 JAVA 下载 HTML 页面?
回答by Klark
Here is the code:
这是代码:
public static String savePage(final String URL) throws IOException {
String line = "", all = "";
URL myUrl = null;
BufferedReader in = null;
try {
myUrl = new URL(URL);
in = new BufferedReader(new InputStreamReader(myUrl.openStream()));
while ((line = in.readLine()) != null) {
all += line;
}
} finally {
if (in != null) {
in.close();
}
}
return all;
}
Now you can process one line after one other in the while loop.
现在您可以在 while 循环中一行一行地处理。
回答by rlovtang
If you have more requirements, like authentication, you can use HttpClient
如果您有更多要求,例如身份验证,则可以使用HttpClient

