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

发布于 2024-08-04 18:01:00 字数 72 浏览 2 评论 0原文

我正在设计一个应用程序,需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋日私语 2024-08-11 18:01:01

如果您只需要读取 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();
    }
}

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.


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();
    }
}
听你说爱我 2024-08-11 18:01:01

如果是 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

冧九 2024-08-11 18:01:01

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

公共类 URLConetent{
公共静态无效主(字符串[]参数){

    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();
    }

}

}

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) {

    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();
    }

}

}

如此安好 2024-08-11 18:01:00

我使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文