sipsorcery
sipsorcery

Reputation: 30714

Translate Ruby snippet to Java

How can I do the equivalent of the Ruby snippet below using Java?

require 'net/http'
res = Net::HTTP.get_response(URI.parse("http://somewhere.com/isalive")).body

Upvotes: 1

Views: 252

Answers (1)

Bozho
Bozho

Reputation: 597324

URL url = new URL("http://somewhere.com/isalive");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();

and then, if you want, you can transform the InputStream to String using IOUtils.toString(inputStream) from apache commons-lang, or something like this.

Update: the above classes should be imported first, with a statement ontop of the class definition:

import java.net.URL;
import java.net.URLConnection;
.. and so on

Upvotes: 1

Related Questions