如何在 Java 中使用 cURL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2586975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:33:13  来源:igfitidea点击:

How to use cURL in Java?

javacurl

提问by moshfiqur

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

Thanks in advance.

提前致谢。

采纳答案by BalusC

You can make use of java.net.URLand/or java.net.URLConnection.

您可以使用java.net.URL和/或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);
    }
}

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

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

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.".

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

See also:

也可以看看:

回答by Stevko

Using standard java libs, I suggest looking at the HttpUrlConnection class http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html

使用标准的 java 库,我建议查看 HttpUrlConnection 类 http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html

It can handle most of what curl can do with setting up the connection. What you do with the stream is up to you.

它可以处理 curl 设置连接所能做的大部分事情。你对流做什么取决于你。

回答by Thorbj?rn Ravn Andersen

Curl is a non-java program and must be provided outside your Java program.

Curl 是一个非 Java 程序,必须在您的 Java 程序之外提供。

You can easily get much of the functionality using Jakarta Commons Net, unless there is some specific functionality like "resume transfer" you need (which is tedious to code on your own)

您可以使用Jakarta Commons Net轻松获得大部分功能,除非您需要某些特定功能,例如“恢复传输”(自己编写代码很乏味)

回答by mjh2007

The Runtimeobject allows you to execute external command line applications from Java and would therefore allow you to use cURL however as the other answers indicate there is probably a better way to do what you are trying to do. If all you want to do is download a file the URL object will work great.

运行时对象允许您从Java执行外部命令行应用程序,因此将允许您使用卷曲然而由于其他的答案表明有可能是一个更好的方式做你正在尝试做的。如果您只想下载一个文件,那么 URL 对象会很好用。

回答by Seth

Some people have already mentioned HttpURLConnection, URLand URLConnection. If you need all the control and extra features that the curl library provides you (and more), I'd recommend Apache's httpclient.

有些人已经提到了HttpURLConnectionURLURLConnection。如果您需要 curl 库为您提供的所有控制和额外功能(以及更多),我建议您使用Apache 的 httpclient