如何使用Java从网站检索URL?

Joh*_*rom 16 java connection url http

我想使用HTTP GET和POST命令从网站检索URL并解析HTML.我该怎么做呢?

Rob*_*ska 20

您可以将HttpURLConnectionURL结合使用.

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

InputStream stream = connection.getInputStream();
// read the contents using an InputStreamReader
Run Code Online (Sandbox Code Playgroud)

  • 使用InputStream创建BufferedReader以将内容读入字符串变量 (2认同)