1

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

问候,

4

4 回答 4

4

我已经使用 Apache Commons HttpClient 库来执行此操作。看看这里: http ://hc.apache.org/httpclient-3.x/tutorial.html

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

于 2009-09-12T04:53:16.543 回答
1

如果您只需要读取 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();
    }
}
于 2009-09-12T05:21:10.487 回答
0

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

JAVA 中的 cURL 等效项

于 2009-09-12T04:49:53.647 回答
0

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

公共类 URLConetent{ 公共静态 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();
    }

}

}

于 2013-02-04T12:22:33.307 回答