Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cURL in Java?

Tags:

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.

Thanks in advance.

like image 640
moshfiqur Avatar asked Apr 06 '10 17:04

moshfiqur


People also ask

Can you use curl in Java?

We can execute curl commands from Java by using the ProcessBuilder — a helper class for building instances of the Process class.

What is the use of curl ()?

cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.

Can I run curl in browser?

With ReqBin Online Curl Client, you can run Curl commands directly from your browser. No desktop apps or browser plugins required. Just enter the Curl command and click on Run. Built-in Curl command syntax Highlighter will highlight Curl command syntax while you type Curl command.


1 Answers

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);     } } 

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.

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

See also:

  • How to use java.net.URLConnection to fire and handle HTTP requests?
  • What are the pros and cons of the leading Java HTML parsers?
like image 92
BalusC Avatar answered Oct 12 '22 23:10

BalusC