tags:

views:

507

answers:

6

Hi, 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.

Thanks in advance.

+4  A: 

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

URL url = new URL("http://stackoverflow.com");
InputStream response = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
for (String line; (line = reader.readLine()) != null;) {
    System.out.println(line);
}
reader.close();

Also see the Sun'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.

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

BalusC
@BalusC: Thanks. Doing some studies from the links you provided. btw, do you know anything better alternative of curl in java, that will provide same functionality like curl? Thanks again.
sparrow
As mentioned, HttpClient is "the better alternative". You can in fact do everything with `URLConnection`, you yet has to know and understand the HTTP specs. By the way, don't try to compare PHP with Java. It's like apples and oranges. To avoid self-troubles/confusions, rather forget about PHP and put a fresh view on Java. It's a language with an entirely different ideology. PS: I do both, so I tell from experience.
BalusC
As this and other people have mentioned use HttpClient. I'd really recommend avoiding using libcurl, you have to make sure you bundle the native library for every platform you want to support and sooner or later someone will be complaining that it doesn't work on their old power mac or SGI workstation.
vickirk
A: 

Using standard java libs, 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 with setting up the connection. What you do with the stream is up to you.

Stevko
+1  A: 

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

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)

Thorbjørn Ravn Andersen
Nice one, but Commons Net goes a bit (too much) further than curl.
BalusC
A: 

The Runtime object 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.

mjh2007
A: 

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

Seth
A: 

My I make a suggestion to use libcurl with Java for your curl methods. Use libcurl straight from within Java programs using this binding. Download the 'curl-java' package from www.gknw.net and use it. Tar Package: http://www.gknw.net/viewvc/trunk/?root=curl-java&view=tar

mpurinton