如何在 Java 中使用 cURL? [英] How to use cURL in Java?

查看:44
本文介绍了如何在 Java 中使用 cURL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,想在 Java 中使用 curl.我的问题是 curl 内置在 Java 中,或者我必须从任何 3rd 方源安装它才能与 Java 一起使用.如果是这样,如何在java中安装curl.我已经在谷歌上搜索了很长时间,但没有找到任何帮助.希望任何人都可以帮助我.

I am a newbie in java and wanted to use curl in java. What is my question is curl built-in in java or I have to install it from any 3rd party source to use with Java. If so, how to install curl in java. I have been googling for a long time but didnt find any help. Hope anyone can help me out there.

提前致谢.

推荐答案

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

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

URL url = new URL("https://stackoverflow.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.".

这篇关于如何在 Java 中使用 cURL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆