开发工具分享
  • 首页
  • 计算科学
  • 文化旅游
  • 项目和网站
    • OSSEZ 计算技术
    • USRealEstate 社区
    • 地区文化
    • CWIKI.US
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
CWIKIUS.CN
一个有独立思考和温度的清新站
Computer Science

Apache HttpClient 使用 POST 方法

可以使用下面的代码将需要查询的内容发送到 Apple.com 查找对话框。 String url = "https://selfsolve.apple.com/wcResults.do"; HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(url); // add header post.setHeader("User-Agent", USER_AGENT); List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("sn", "C02G8416DRJM")); urlParameters.add(new BasicNameValuePair("cn", "")); urlParameters.add(new BasicNameValuePair("locale", "")); urlParameters.add(new BasicNameValuePair("caller", "")); urlParameters.add(new BasicNameValuePair("num", "12345")); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); }  

2018年11月09日 0Comments 3741Browse 0Like Read more
Computer Science

Apache HttpClient 使用 Get 方法

如何使用 Apache HttpClient 使用 Get 方法从网站上下载数据。 可以使用下面的方法来获得 Google 的查询结果。 String url = "http://www.google.com/search?q=httpClient"; HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); // add request header request.addHeader("User-Agent", USER_AGENT); HttpResponse response = client.execute(request); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); }  

2018年11月09日 0Comments 1013Browse 1Like Read more
Computer Science

Jersey 2.x 运行项目

现在我们已经有可以可以运行的项目了,让我们队这个项目进行一些测试吧。 你需要运行下面的一些命令行: mvn clean test 这个命令将会对项目进行编译后运行单元测试。 你应该会看到和下面类似的输出表示项目编译成功了: Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.742 s [INFO] Finished at: 2018-03-27T15:05:50-04:00 [INFO] Final Memory: 17M/205M [INFO] ------------------------------------------------------------------------ 上面的输出表示的是项目已经被编译测试通过了。 我们可以开始使用独立启动方式启动项目了,希望直接启动项目,需要运行下面的 mvn 项目启动命令: mvn exec:java 这时候,项目应该已经正常启动了,很快你应该可以在控制台上看到下面的输出: Mar 27, 2018 3:10:28 PM org.glassfish.grizzly.http.server.NetworkListener start INFO: Started listener bound to [localhost:8080] Mar 27, 2018 3:10:28 PM org.glassfish.grizzly.http.server.HttpServer start INFO: [HttpServer] Started. Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl Hit enter to stop it... 项目这个时候已经运行了,有关项目的 WADL 描述文件可以通过 http://localhost:8080/myapp/application.wadl URL 访问到。 你可以考虑在你的控制台中通过命令中 curl http://localhost:8080/myapp/application.wadl 访问这个项目的描述文件,你也可以直接将这个链接拷贝粘贴到浏览器中直接进行查看。 如果一切正常的话,你应该可以看到你部署的 RESTful 应用程序的的 WADL 格式的 XML 文档。希望查看更多有关 WADL 的内容,请查看章节  Chapter 17, WADL Support。 部署成功后最后一件可以尝试的事情就是与部署的 /myresource 资源进行数据交互。 你可以考虑直接把  http://localhost:8080/myapp/myresource 链接复制粘贴到浏览器中,你也可以通过 curl 执行命令。 有关 curl 执行命令的结果如下: $ curl http://localhost:8080/myapp/myresource Got it! 你可以看到,当你执行上面的命令后,控制台输出了 Got it!消息,这个消息是服务器发送给我们的资源。 我们也可以通过参数 -i 让 curl 提供更多的信息给我们来让我们了解有关消息发送响应的相关信息。 curl -i http://localhost:8080/myapp/myresource HTTP/1.1 200 OK Content-Type: text/plain Date: Sun, 26 May 2013 18:27:19 GMT Content-Length: 7 Got it! 注意到Content-Type: text/plain是在 MyResource 类中用@Produces 注解的。 如果想看到更多返回信息,或者想了解 curl 客户端和运行的 Grizzly I/O 容器的交互,可以变换不同的 curl 命令参数。例如下面的例子可以让 curl 客户端输出更多有关于服务器通信和交互的相关信息: $ curl -v http://localhost:8080/myapp/myresource * About to connect() to localhost port 8080 (#0) * Trying ::1... * Connection refused * Trying 127.0.0.1... * connected * Connected to localhost (127.0.0.1) port 8080 (#0) > GET /myapp/myresource HTTP/1.1 > User-Agent: curl/7.25.0 (x86_64-apple-darwin11.3.0) libcurl/7.25.0 OpenSSL/1.0.1e zlib/1.2.7 libidn/1.22 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: text/plain < Date: Sun, 26 May 2013 18:29:18 GMT < Content-Length: 7 < * Connection #0 to host localhost left intact Got it!* Closing connection #0 https://www.cwiki.us/display/JERSEYZH/Running+the+Project

2018年11月08日 0Comments 1254Browse 0Like Read more
Computer Science

Jersey 2.x 探索新建的工程

如果用 Jersey maven archetype 成功创建了这个项目,那么在你当前的路径下就已经创建了一个名为simple-service项目。它包含了一个标准的Maven项目结构: 说明 文件目录 在项目根目录下的的项目构建和管理配置描述文件  pom.xml 项目源代码文件 src/main/java 项目测试源代码 src/test/java 在原文路径下的com.example包中有两个 class 文件,这个 Main 类主要是负责启动 Grizzly 容器,同时也为这个容器配置和部署 JAX-RS 应用。 在同一个包内的另外一个类 MyResource 类是 JAX-RS 的一个实现的源代码,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.example;   import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;   /**  * Root resource (exposed at "myresource" path)  */ @Path("myresource") public class MyResource {       /**      * Method handling HTTP GET requests. The returned object will be sent      * to the client as "text/plain" media type.      *      * @return String that will be returned as a text/plain response.      */     @GET     @Produces(MediaType.TEXT_PLAIN)     public String getIt() {         return "Got it!";     } } 一个 JAX-RS 资源是一个可以处理绑定了资源的URI的HTTP请求的带有注解的POJO,详细内容可以看第三章。在我们的例子中,单一的资源暴露了一个公开的方法,能够处理HTTP GET请求,绑定在/myresourceURI路径下,可以产生媒体类型为“text/plain”的响应消息。在这个示例中,资源返回相同的“Got it!”应对所有客户端的要求。 在src/test/java目录下的 MyResourceTest 类是对 MyResource 的单元测试,他们具有相同的包 com.example 中。但是这个单元测试类被放置到 maven 工程中的测试源代码目录下 src/test/java(为了增加代码的简洁性,一些代码的注释和 JUnit 的导入类被省略了)。 package com.example;   import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget;   import org.glassfish.grizzly.http.server.HttpServer;   ...   public class MyResourceTest {       private HttpServer server;     private WebTarget target;       @Before     public void setUp() throws Exception {         server = Main.startServer();           Client c = ClientBuilder.newClient();         target = c.target(Main.BASE_URI);     }       @After     public void tearDown() throws Exception {         server.stop();     }       /**      * Test to see that the message "Got it!" is sent in the response.      */     @Test     public void testGetIt() {         String responseMsg = target.path("myresource").request().get(String.class);         assertEquals("Got it!", responseMsg);     } } 在这个单元测试中静态方法 Main.startServer()首先将 Grizzly 容器启动,然后服务器应用部署到测试中的 setUp() 方法。接下来,一个JAX-RS 客户端组件在相同的测试方法创建。 先是一个新的JAX-RS客户端实例生成,接着JAX-RS web target 部件指向我们部署的应用程序上下文的 root:http://localhost:8080/myapp/( Main.BASE_URI 的常量值)存储在单元测试类目标区。这个区被用于实际的单元测试方法(testgetit())。 在 testgetit() 方法中,JAX-RS 客户端 API 是用来连接并发送 HTTP GET 请求的 MyResource JAX-RS 资源类侦听在/myresource 的URI。同样作为 JAX-RS API 方法调用链的一部分,回应以Java字符串类型被读到。在测试方法的第二行,响应的内容(从服务器返回的字符串)跟测试断言预期短语比较。要了解更多有关使用 JAX-RS 客户端API,请参阅第5章客户端API。 https://www.cwiki.us/display/JERSEYZH/Exploring+the+Newly+Created+Project

2018年11月08日 0Comments 1089Browse 0Like Read more
Computer Science

Jersey 2.x 从Maven Archetype 创建一个新项目

创建 Jersey 工程需要使用 Apache 的 Maven 软件工程和管理工具。所有的Jersey产品模块都可以在 Maven中央库 中找到。这样的话 Jersey 可以非常容易和其他基于 Maven 的项目进行配置(non-SNAPSHOT)。 Jersey 已经部署到中央仓库中了,因此你不需要配置其他仓库来让 Jersey 可以工作。 有关 SNAPSHOT 版本 如果你想要使用最新的 Jersey 模块的 SNAPSHOT 版本(SNAPSHOT 版本代表不稳定、尚处于开发中的版本),需要在 pom.xml 中添加如下内容: <repository>     <id>snapshot-repository.java.net</id>     <name>Java.net Snapshot Repository for Maven</name>     <url>https://maven.java.net/content/repositories/snapshots/</url>     <layout>default</layout> </repository> 使用 Maven 的工程创建一个 Jersey 项目是最方便的,让我们用这种方法来看一下它是怎么实现的。让我们创建一个新的 Jersey 项目,运行在Grizzly容器。 我们使用 Jersey-provided 的 maven archetype。创建一个项目,需要执行下面的代码: mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \ -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \ -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \ -DarchetypeVersion=2.26 在你的项目里面随意调整 pom.xml 内的 groupId,包名和版本号就可以成为一个新的项目。   https://www.cwiki.us/display/JERSEYZH/Creating+a+New+Project+from+Maven+Archetype

2018年11月08日 0Comments 1147Browse 0Like Read more
Computer Science

Jersey 2.x 服务器端应用支持的容器

基于 JAX-RS Servlet-based 部署的一部分标准,能运行在任何支持 Servlet 2.5 和更高标准的的容器上。Jersey 提供支持程序化部署在下面的容器中:Grizzly 2 (HTTP 和 Servlet), JDK Http server,Simple Http server 和 Jetty Http server 本章节仅仅提供了需要 Maven 提供的依赖,有关更多的细节,请参考 Chapter 4, Application Deployment and Runtime Environments 页面中的内容。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-grizzly2-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-grizzly2-servlet</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-jdk-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-simple-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-jetty-http</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <artifactId>jersey-container-jetty-servlet</artifactId>     <version>2.27</version> </dependency> https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月07日 0Comments 1046Browse 0Like Read more
Computer Science

Jersey 2.x JDK 上的客户端应用

如应用是运行在 JDK 上的话,你只需要使用 JAX-RS 中的客户端部分就可以了,这个根据你使用的客户端有所调整。 这里有一系列的模块是可以供你使用的,例如 grizzly 或 apache 或 jetty connector(请参考下面的的依赖表格)。Jersey 客户端默认使用 JDK 进行运行(使用的是 HttpUrlConnection)。 请参考 Chapter 5, Client API 获得更多的细节。 1 2 3 4 5 <dependency>     <groupId>org.glassfish.jersey.core</groupId>     <artifactId>jersey-client</artifactId>     <version>2.27</version> </dependency> 当前可用的连接器: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependency>     <groupId>org.glassfish.jersey.connectors</groupId>     <artifactId>jersey-grizzly-connector</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.connectors</groupId>     <artifactId>jersey-apache-connector</artifactId>     <version>2.27</version> </dependency>   <dependency>     <groupId>org.glassfish.jersey.connectors</groupId>     <artifactId>jersey-jetty-connector</artifactId>     <version>2.27</version> </dependency> https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月07日 0Comments 1200Browse 0Like Read more
Computer Science

Jersey 2.x 基于 Servlet 的服务器端应用

下面的依赖通常应用到应用服务器上(servlet 容器),同时这个应用服务器上没有整合任何 JAX-RS 的实现。 因此,这个应用服务器需要包含有 JAX-RS API 和 Jersey 实现,同时部署到服务器上。 1 2 3 4 5 6 7 8 9 10 11 12 <dependency>     <groupId>org.glassfish.jersey.containers</groupId>     <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core"  -->     <artifactId>jersey-container-servlet</artifactId>     <version>2.27</version> </dependency> <!-- Required only when you are using JAX-RS Client --> <dependency>     <groupId>org.glassfish.jersey.core</groupId>     <artifactId>jersey-client</artifactId>     <version>2.27</version> </dependency>   https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月07日 0Comments 1111Browse 0Like Read more
Computer Science

Jersey 2.x Glassfish 中基于 Servlet 的应用

如果你使用的是 Glassfish 作为你应用服务器,你不需要在你的引用中包含引用任何东西,所有你需要的都已经包含进去了。 你只需要定义 JAX-RS API 以便于你能够对你的应用进行编辑,使用 (provided)依赖。 1 2 3 4 5 6 <dependency>     <groupId>javax.ws.rs</groupId>     <artifactId>javax.ws.rs-api</artifactId>     <version>2.1</version>     <scope>provided</scope> </dependency> 如果你需要使用 Jersey 的一些特定特性,你需要基于你的 Jersey 目录进行添加。 1 2 3 4 5 6 7 8 9 10 11 12 13 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.27</version> <scope>provided</scope> </dependency> <!-- if you are using Jersey client specific features without the server side --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.27</version> <scope>provided</scope> </dependency> https://www.cwiki.us/display/JERSEYZH/Common+Jersey+Use+Cases

2018年11月06日 0Comments 985Browse 0Like Read more
Computer Science

Jersey 2.x 前言和约定的文本格式

这是Jersey 2.x 的用户指南。我们极力将它能与我们新增的功能保持一致。当阅读本指南,作为补充,也请移步至 Jersey API documentation 查看 Jersey 的特性和 API。 欢迎任何对本指南的建议和提问,可以联系users@jersey.java.net,同样的,发现勘误,也可以在Jersey JIRA Issue Tracker 提问,请注意需要在 docs 文档组件部分中进行提问。 请注意,在提问的时候需要注明你有疑问的 Jersey 用户指南的版本,你在哪里发现的错误,同时请注明影响到的那个版本。 约定的文本格式 首先需要说明的是,任何 Jersey 和 JAX-RS API 的组件将会在参考组件中链接到 API 文档。任何组件下面的子组件将会使用非寸线字体。 Emphasised 字体被使用在第一次出现的介绍中。 在一些代码的列表中,有些代码太长而不能在一行中完整显示下来的话,代码行将会扩展到下一页或者下一段中,我们将会用  '\'  标识在每一行的页尾部用于表示这些内容应该是在一行中显示的。 例如: This is an overly long line that \ might not fit the available page \ width and had to be broken into \ multiple lines. This line fits the page width. (这一行的字符能够在一行中完整显示) 上面的内容应该阅读成下面的内容: This is an overly long line that might not fit the available page width and had to be broken into multiple lines. This line fits the page width. 换句话说如果每一行的尾部有斜杠,则表示的是需要将下一行的内容拼接到上以后来进行完整的阅读。   https://www.cwiki.us/display/JERSEYZH/Preface

2018年11月06日 0Comments 996Browse 0Like Read more
1…246247248249250…304
Archives
  • June 2026
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
Categories
  • Computer Science (2,367)
    • Confluence (663)
    • Gradle (12)
  • U.S. (523)
  • 文化旅游 (146)

COPYRIGHT © 2020 CWIKIUS. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

湘ICP备2020018253号-1