且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Java 中使用 cURL?

更新时间:2023-02-24 13:59:13

您可以使用 java.net.URL 和/或 java.net.URLConnection.

You can make use of java.net.URL and/or java.net.URLConnection.

URL url = new URL("https://***.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

另请参阅有关该主题的Oracle 的简单教程.然而,它有点冗长.为了减少冗长的代码,您可能需要考虑 Apache HttpClient相反.

Also see the Oracle's simple tutorial on the subject. It's however a bit verbose. To end up with less verbose code, you may want to consider Apache HttpClient instead.

顺便说一句:如果您的下一个问题是如何处理 HTML 结果?",那么答案是使用 HTML 解析器.不,不要为此使用正则表达式.".

By the way: if your next question is "How to process HTML result?", then the answer is "Use a HTML parser. No, don't use regex for this.".