如何使用 Java 从服务器端的特定 URL 获取 HTML 内容?
我正在设计一个应用程序,需要使用 Java 从服务器端的特定 URL 加载 HTML 内容。我该如何解决?
问候,
I am designing an application that needs to load HTML content from a specific URL on server side by using Java. How can I solve it?
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只需要读取 url,则无需求助于第三方库,java 内置了对检索 url 的支持。
If all you need is read the url you do not need to resort to third party libraries, java has built in support to retrieve urls.
如果是 php,您可以使用 cURL,但由于它是 java,您将使用 HttpURLConnection,正如我刚刚发现的这个问题:
JAVA 中的 cURL 等效项
If it was php, you could use cURL, but since it's java, you would use HttpURLConnection, as I just found out on this question:
cURL equivalent in JAVA
导入 java.io.BufferedReader;
导入java.io.IOException;
导入 java.io.InputStreamReader;
导入 java.net.MalformedURLException;
导入java.net.URL;
导入 java.net.URLConnection;
公共类 URLConetent{
公共静态无效主(字符串[]参数){
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLConetent{
public static void main(String[] args) {
}
我使用 Apache Commons HttpClient 库来执行此操作。看看这里:
http://hc.apache.org/httpclient-3.x/tutorial。 html
它比 JDK HTTP 客户端支持功能更丰富。
I have used the Apache Commons HttpClient library to do this. Have a look here:
http://hc.apache.org/httpclient-3.x/tutorial.html
It is more feature rich than the JDK HTTP client support.