如何使用 Java 从服务器端的特定 URL 获取 HTML 内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1414302/
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-29 16:28:03  来源:igfitidea点击:

How can I get HTML content from a specific URL on server side by using Java?

javaweb-scraping

提问by Arthur Ronald

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?

我正在设计一个需要使用 Java 从服务器端的特定 URL 加载 HTML 内容的应用程序。我该如何解决?

Regards,

问候,

回答by David Tinker

I have used the Apache Commons HttpClient library to do this. Have a look here: http://hc.apache.org/httpclient-3.x/tutorial.html

我使用了 Apache Commons HttpClient 库来做到这一点。看看这里:http: //hc.apache.org/httpclient-3.x/tutorial.html

It is more feature rich than the JDK HTTP client support.

它比 JDK HTTP 客户端支持功能更丰富。

回答by Hamza Yerlikaya

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.

如果您只需要读取 url,则不需要求助于第三方库,java 内置了对检索 url 的支持。


import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

回答by Anthony

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:

如果是 php,您可以使用cURL,但由于它是 java,您将使用HttpURLConnection,正如我刚刚在这个问题上发现的:

cURL equivalent in JAVA

JAVA 中的 cURL 等价物

回答by Vaibs

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection;

导入 java.io.BufferedReader; 导入 java.io.IOException; 导入 java.io.InputStreamReader; 导入 java.net.MalformedURLException; 导入 java.net.URL; 导入 java.net.URLConnection;

public class URLConetent{ public static void main(String[] args) {

公共类 URLConetent{ public static void main(String[] args) {

    URL url;

    try {
        // get URL content

        String a="http://localhost:8080//TestWeb/index.jsp";
        url = new URL(a);
        URLConnection conn = url.openConnection();

        // open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(
                           new InputStreamReader(conn.getInputStream()));

        String inputLine;
        while ((inputLine = br.readLine()) != null) {
                System.out.println(inputLine);
        }
        br.close();

        System.out.println("Done");

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

}