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

标签 java jersey-client

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

最佳答案

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

  1. URLConnection 客户端

    导入java.io.BufferedReader; 导入java.io.IOException; 导入 java.io.InputStreamReader; 导入java.net.URL; 导入 java.net.URLConnection; 公共(public)类 URLConnectionClient { 公共(public)静态无效主(字符串[] args)抛出IOException { 网址restURL =新网址(“http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext”); URLConnection连接=(URLConnection)restURL.openConnection(); 连接.setDoOutput(true); 连接.connect(); InputStreamReader ins = new InputStreamReader(connection.getInputStream()); BufferedReader in = new BufferedReader(ins); 字符串输入行; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } 附寄(); } }

  2. HttpConnection 客户端

    导入java.io.BufferedReader; 导入java.io.IOException; 导入 java.io.InputStreamReader; 导入 java.net.HttpURLConnection; 导入java.net.URL; 公共(public)类 HttpConnectionClient { 公共(public)静态无效主(字符串[] args)抛出IOException { 网址restURL =新网址(“http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext”); HttpURLConnection 连接 = (HttpURLConnection)restURL.openConnection(); 连接.setRequestMethod("GET"); 连接.setReadTimeout(10000); 连接.connect(); InputStreamReader ins = new InputStreamReader(connection.getInputStream()); BufferedReader in = new BufferedReader(ins); 字符串输入行; while ((inputLine = in.readLine())!=null) { System.out.println(inputLine); }

    } }

  3. URL 流客户端

    导入java.io.BufferedReader; 导入java.io.IOException; 导入 java.io.InputStreamReader; 导入java.net.URL; 公共(public)类 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); 字符串输入行; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } 附寄(); } }

  4. Jersey 客户端。

    导入javax.ws.rs.client.Client; 导入 javax.ws.rs.client.ClientBuilder; 导入 javax.ws.rs.client.Initation.Builder; 导入 javax.ws.rs.client.WebTarget; 导入 javax.ws.rs.core.Response; 公共(public)类 URLJerseyClient { 公共(public)静态无效主(字符串[] args){ 客户端 cl = ClientBuilder.newClient(); WebTarget target = cl.target("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext "); 目标.路径(“资源”); 生成器 requestBuilder = target.request(); 响应response = requestBuilder.get(); System.out.println(response.getStatus()); System.out.println(response.readEntity(String.class)); } }

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

<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

关于java - 如何在java中使用jersey客户端获取网页的页面源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849294/

相关文章:

java - 比较 java.lang.Class 和 javax.lang.model.element.TypeElement 的对象

java - Android:BroadcastReceiver:UNINSTALL_PACKAGE Intent

Java - Python - Python 中是否有与 java 的 Character.getNumericValue 等效的东西?

java - 将 Jersey 客户端转换为 HttpUrlConnection

java - wadl2java 生成的客户端在成功发布和放置时抛出 UniformInterfaceException

java - 如何使用 Java 在 Axe-Core 中运行特定标签的可访问性测试

JavaFX 应用程序线程到任务通信

java - 泽西客户端握手错误

java - Jersey 客户端无法指定媒体类型

java - Restful Web 服务出错