`
小于Brick
  • 浏览: 88976 次
  • 性别: Icon_minigender_1
  • 来自: 新疆
社区版块
存档分类
最新评论

grails3 httpClient4.5.1 基本使用

阅读更多

在grails3 中引入httpclient相关jar

 

apache 的client-4.5.x依赖列表:

http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/dependency-info.html

 

grails 的httpclient插件地址:

https://repo.grails.org/grails/core/org/apache/httpcomponents/httpclient/

 

注意:这里build.gradle文件中我刚开始引入的是4.5.2,而且grails库中也是有的,但编译器提示找不到资源不能下载:(我grails使用的版本是3.1.5)

compile 'org.apache.httpcomponents:httpclient:4.5.2'

 换成4.5.1后通过

compile 'org.apache.httpcomponents:httpclient:4.5.1'

 

 httpclient 4.5.2 API:

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/

 

apache提供的示例:

http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html

package org.apache.http.examples.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class QuickStart {

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet("http://httpbin.org/get");
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            // The underlying HTTP connection is still held by the response object
            // to allow the response content to be streamed directly from the network socket.
            // In order to ensure correct deallocation of system resources
            // the user MUST call CloseableHttpResponse#close() from a finally clause.
            // Please note that if response content is not fully consumed the underlying
            // connection cannot be safely re-used and will be shut down and discarded
            // by the connection manager.
            try {
                System.out.println(response1.getStatusLine());
                HttpEntity entity1 = response1.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity1);
            } finally {
                response1.close();
            }

            HttpPost httpPost = new HttpPost("http://httpbin.org/post");
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("username", "vip"));
            nvps.add(new BasicNameValuePair("password", "secret"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            CloseableHttpResponse response2 = httpclient.execute(httpPost);

            try {
                System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity2);
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    grails使用freemarker.rar

    标题中的“grails使用freemarker.rar”表明这是一个关于如何在Grails框架中应用FreeMarker模板引擎的资源包。FreeMarker是一个开源的、基于Java的模板引擎,它用于生成动态HTML或其他格式的文本,比如XML、PDF等。...

    grails3 CAS链接接数据库

    标题中的“grails3 CAS链接接数据库”表明我们要讨论的是如何在Grails 3框架下集成CAS(Central Authentication Service)并实现与数据库的连接。Grails是一个基于Groovy语言的开源Web应用框架,而CAS是一种广泛使用...

    111D:\keke\grails指南\笔记\1111111.txt,grails 初学者,使用说明的。

    从给定的文件信息来看,主要关注点在于Grails框架的初学者使用说明。Grails是一个基于Groovy语言的开源Web应用开发框架,它利用了Java平台的优势,并结合了动态语言的灵活性,使得Web开发变得更为高效、简洁。下面将...

    使用 Grails 快速开发 Web 应用程序

    通过本教程,读者将掌握Grails的基本开发技术,包括Groovy语言基础、MVC模式的理解以及Grails框架的核心组件使用。同时,也将具备进一步探索Grails高级特性的基础,如插件系统、国际化、安全性和测试等。对于熟悉...

    一步一步学grails(3)

    本教程以一个实际项目为例,介绍如何使用 Grails 来开发 Web 应用程序。 #### 二、Grails 项目初始化与领域模型设计 在项目开始阶段,我们首先需要创建一个名为 `ProductionNo` 的领域模型。该模型代表了一个特定...

    Eclipse下搭建Grails项目

    - 由于Grails项目涉及GSP(Groovy Server Pages)文件,推荐使用包含JSP编辑器的Eclipse JEE版本。如果你对JSP语法熟悉且内存有限,也可选择Classic版本。 - 安装Eclipse 3.4.0 JEE版本或其他支持JSP编辑的最新...

    Grails 3开发邮件发送功能

    在`build.gradle`文件中的dependencies部分,你需要添加邮件服务依赖库,这里使用的依赖库为`org.grails.plugins:mail:2.0.0.RC6`。 3. 接下来需要配置邮件服务相关的参数,这些参数包括SMTP服务器的地址、端口号、...

    Grails Grails Grails

    《Grails1.1中文文档》是Grails 1.1版本的官方中文指南,包含了框架的详细介绍、安装指南、基本概念、MVC模式的应用、GORM的使用、服务层、控制器、视图、国际化、测试等方面的内容。通过阅读这份文档,开发者可以...

    Grails3配置单独的数据库properties.pdf

    3. **配置数据源类**:Grails 3使用Spring Boot,所以数据源的配置也遵循Spring Boot的规则。这意味着你需要在`build.gradle`文件中包含对应的数据库驱动依赖。例如,对于MySQL,你可能需要添加: ```gradle ...

    Groovy轻松入门——Grails实战基础篇

    但是通常情况下,建议还是配置`GROOVY_HOME`环境变量,这样可以更好地管理和使用Groovy工具。 ### 二、创建Grails Demo程序 完成Grails环境的搭建后,我们就可以开始创建一个简单的Grails应用程序了。 1. **打开...

    grails 开发框架-3

    grails1.0开发框架3 类似于ruby on rails的框架。

    使用GORM构建Spring Boot应用程序 Grails指南_ Grails框架.pdf

    在本教程中,我们将探讨如何使用GORM(Grails Object-Relational Mapping)构建Spring Boot应用程序。GORM是Grails框架的核心部分,它为开发者提供了简单、强大的数据持久化功能,支持Hibernate和MongoDB等数据库。...

    Grails权威指南

     4.6.1 使用get方法和exists方法进行基本查询  4.6.2 列举(listing),70排序(sorting)以及合计(counting)  4.6.3 用动态查找器(finder)查询  4.6.4 使用hql进行查询  4.6.5 按样本...

    Grails开源框架 - 使用指南

    自己制作的《Grails开源框架 - 使用指南》CHM电子书

    使用Grails快速开发Web应用

    - **创建应用**:使用`grails create-app Contact`命令可快速生成名为“Contact”的应用框架,该命令自动生成了标准的项目目录结构,包括但不限于`grails-app`下的`conf`、`controller`、`domain`、`i18n`、`...

    eclipse开发grails插件

    3. **配置Grails环境**:安装插件后,需要在Eclipse中配置Grails的路径,这样Eclipse才能找到Grails命令行工具。这通常在"Eclipse Preferences" -&gt; "Grails"中设置。 4. **创建Grails项目**:现在,你可以通过...

Global site tag (gtag.js) - Google Analytics