How to use cURL in Java?

I am new to java and wanted to use curl in java. What is my question - is it embedded in java or should I install it from any third-party source for use with Java. If so, how to set curl in java. I searched Google for a long time, but did not find any help. Hope someone can help me.

Thanks in advance.

+81
java curl
Apr 6 '10 at 17:37
source share
5 answers

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

 URL url = new URL("http://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 a simple Oracle tutorial on this. This, however, is a bit verbose. For more detailed code, you can consider Apache HttpClient instead .

By the way, if your next question is: “How to handle the HTML result?”, Then the answer is “ Use an HTML parser . No, do not use regex for this .

See also:

+127
Apr 6 '10 at 17:41
source share

Some people have already mentioned HttpURLConnection , URL and URLConnection . If you need all the controls and extra features that the curl library provides (and much more), I would recommend Apache httpclient .

+4
Apr 6 2018-10-06T00:
source share

The Runtime object allows you to run external command-line applications with Java and therefore allows you to use cURL, however, since other answers indicate that there is probably a better way to do what you are trying to do. If all you want to do is upload a file, the URL object will work just fine.

+3
Apr 6 '10 at 17:48
source share

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

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

+2
Apr 6 2018-10-06T00:
source share

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

You can easily get most of the functionality using Jakarta Commons Net if you don’t have certain features like the “resume transfer” you need (which is tedious for the code yourself)

+2
Apr 06 2018-10-06T00:
source share



All Articles