Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Web-Service / Website from Java

Writing some additional classes for an existing GWT project. I need to:

  • Request a URL
  • Read in the webpage returned, in order to perform operations on.

The returned page is in very simple HTML, therefore parsing it shouldn't be very difficult, I just need to get the data first.

How do I do this in Java? What packages am I best looking at?

like image 886
Federer Avatar asked Jul 28 '26 16:07

Federer


2 Answers

With native Java API, the easiest way to read from an URL is using java.net.URL#openStream(). Here's a basic example:

try (InputStream response = new URL("https://www.stackoverflow.com").openStream()) {
    String body = new String(input.readAllBytes(), StandardCharsets.UTF_8);
    System.out.println(body);
}

You could feed the InputStream to any DOM/SAX parser of your taste. The average parser can take (in)directly an InputStream as argument or even a URL. Jsoup is one of the better HTML parsers.

In case you want a bit more control and/or want a more self-documenting API, then you can since Java 11 use the java.net.http.HttpClient. It only gets verbose quickly when you merely want the response body:

HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("https://stackoverflow.com")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String body = response.body();
System.out.println(body);

See also:

  • How to use java.net.URLConnection to fire and handle HTTP requests
like image 54
BalusC Avatar answered Jul 30 '26 04:07

BalusC


For HTML pages you should use HttpClient.

For Web services, you need a framework like CXF.

like image 36
kgiannakakis Avatar answered Jul 30 '26 05:07

kgiannakakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!