Search code examples
javajersey-client

How to get the page source of a webpage using jersey client in java


In my web application using java, I am trying to get the page source of a web page using jersey client by passing the URL of the required page. I have been searching the web to find some good examples that would help me, but couldn't find any. Can anybody help me with this.


Solution

  • Jersey is for web services. But in general, you can get the HTML source. All these 4 varieties of jax-rs clients will print you the code:

    1. URLConnection client

      import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class URLConnectionClient { public static void main(String[] args) throws IOException { URL restURL = new URL("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext"); URLConnection connection = (URLConnection) restURL.openConnection(); connection.setDoOutput(true); connection.connect(); InputStreamReader ins = new InputStreamReader(connection.getInputStream()); BufferedReader in = new BufferedReader(ins); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } }

    2. HttpConnection client

      import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpConnectionClient { public static void main(String[] args) throws IOException { URL restURL = new URL("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext"); HttpURLConnection connection = (HttpURLConnection) restURL.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(10000); connection.connect(); InputStreamReader ins = new InputStreamReader(connection.getInputStream()); BufferedReader in = new BufferedReader(ins); String inputLine; while ((inputLine = in.readLine())!=null) { System.out.println(inputLine); }

      } }

    3. URL stream client

      import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class URLOpenClient {

      public static void main(String[] args) throws IOException { URL restURL = new URL("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext"); InputStreamReader ins = new InputStreamReader(restURL.openStream()); BufferedReader in = new BufferedReader(ins); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } }

    4. Jersey client.

      import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; public class URLJerseyClient { public static void main(String[] args) { Client cl = ClientBuilder.newClient(); WebTarget target = cl.target("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext"); target.path("resource"); Builder requestBuilder = target.request(); Response response = requestBuilder.get(); System.out.println(response.getStatus()); System.out.println(response.readEntity(String.class)); } }

    For this one you will need a dependency:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.vvirlan</groupId>
    <artifactId>cert</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Client</name>
    
    <dependencies>
    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.21</version>
    </dependency>
    </dependencies>
    </project>
    

    4 JAX-RS clients