如何在java中使用jersey客户端获取网页的页面源

时间:2015-09-29 16:33:56

标签: java jersey-client

在我使用java的Web应用程序中,我试图通过传递所需页面的URL来获取使用jersey客户端的网页的页面源。我一直在网上搜索一些可以帮助我的好例子,但找不到任何好的例子。 任何人都可以帮助我。

2 个答案:

答案 0 :(得分:1)

Jersey适用于网络服务。但一般来说,您可以获取HTML源代码。 所有这四种jax-rs客户端都会打印代码:

  1. URLConnection客户端

    import java.io.BufferedReader;     import java.io.IOException;     import java.io.InputStreamReader;     import java.net.URL;     import java.net.URLConnection;     公共类URLConnectionClient {     public static void main(String [] args)throws IOException {     网址restURL =新网址(“http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext”);     URLConnection connection =(URLConnection)restURL.openConnection();     connection.setDoOutput(真);     connection.connect();     InputStreamReader ins = new InputStreamReader(connection.getInputStream());     BufferedReader in = new BufferedReader(ins);     String inputLine;     while((inputLine = in.readLine())!= null){     的System.out.println(inputLine);     }     附寄();     }     }

  2. HttpConnection客户端

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; 公共类HttpConnectionClient { public static void main(String [] args)throws IOException { 网址restURL =新网址(“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. 网址流客户端

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; 公共类URLOpenClient {

    public static void main(String [] args)抛出IOException { 网址restURL =新网址(“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); } 附寄(); } }

  4. 泽西岛客户。

    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; 公共类URLJerseyClient { public static void main(String [] args){ 客户端cl = ClientBuilder.newClient(); WebTarget target = cl.target(“http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext”); target.path( “资源”); Builder requestBuilder = target.request(); 响应响应= requestBuilder.get(); 的System.out.println(response.getStatus()); 的System.out.println(response.readEntity(String.class)); } }

  5. 对于这个,你需要一个依赖:

    <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

答案 1 :(得分:0)

如果您的目的只是下载html代码(不渲染它),您可以使用任何普通的http客户端(甚至是java URLConnection类)

下面是我在我的一个工具中使用的一个现成样本。它使用apache http core 4.1.4和apache http client 4.1.4。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUtil {

    public static String getFile(String sUrl) throws ClientProtocolException, IOException{
        HttpClient httpclient = new DefaultHttpClient();
        StringBuilder b = new StringBuilder();

         // Prepare a request object
         HttpGet httpget = new HttpGet(sUrl);

         // Execute the request
         HttpResponse response = httpclient.execute(httpget);

         // Examine the response status
         System.out.println(response.getStatusLine());

         // Get hold of the response entity
         HttpEntity entity = response.getEntity();

         // If the response does not enclose an entity, there is no need
         // to worry about connection release
         if (entity != null) {
             InputStream instream = entity.getContent();
             try {

                 BufferedReader reader = new BufferedReader(
                         new InputStreamReader(instream));
                 // do something useful with the response
                 String s = reader.readLine();
                 while(s!= null){
                     b.append(s);
                     b.append("\n");
                     s = reader.readLine();
                 }
             } catch (IOException ex) {

                 // In case of an IOException the connection will be released
                 // back to the connection manager automatically
                 throw ex;

             } catch (RuntimeException ex) {

                 // In case of an unexpected exception you may want to abort
                 // the HTTP request in order to shut down the underlying
                 // connection and release it back to the connection manager.
                 httpget.abort();
                 throw ex;

             } finally {

                 // Closing the input stream will trigger connection release
                 instream.close();

             }

             // When HttpClient instance is no longer needed,
             // shut down the connection manager to ensure
             // immediate deallocation of all system resources
             httpclient.getConnectionManager().shutdown();
         }
         return b.toString();
    }

}