随着spring boot越来越来流行,restful风格的接口也成为新的接口规范,spring MVC项目调用restful接口的工具类

import com.sun.xml.internal.fastinfoset.Encoder;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;import java.io.IOException;
import java.net.URI;public class HttpUtils {/*** @param url eg:http://localohost:8080/web/user/1*            http://localohost:8080/web/user?id=1* @return*/public static String doGet(String url) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();// 返回结果String result = null;// 执行url之后的响应CloseableHttpResponse response = null;try {// 创建uriURIBuilder builder = new URIBuilder(url);URI uri = builder.build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// 执行请求response = httpClient.execute(httpGet);result = EntityUtils.toString(response.getEntity(), "UTF-8");} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpClient.close();} catch (IOException e) {e.printStackTrace();}}return result;}/*** @param url  eg:http://localohost:8080/web/user* @param json eg:{"name":"aa","id":"1","password":"123"}* @return*/public static String doPost(String url, String json) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String result = null;try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建请求内容StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);// 执行http请求response = httpClient.execute(httpPost);result = EntityUtils.toString(response.getEntity(), Encoder.UTF_8);} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpClient.close();} catch (IOException e) {e.printStackTrace();}}return result;}/*** @param url  eg:http://localohost:8080/web/user* @param json eg:{"name":"AA","id":"1"}* @return*/public static String doPut(String url, String json) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String result = null;try {// 创建Http Post请求HttpPut httpPut = new HttpPut(url);// 创建请求内容StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPut.setEntity(entity);// 执行http请求response = httpClient.execute(httpPut);result = EntityUtils.toString(response.getEntity(), Encoder.UTF_8);} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpClient.close();} catch (IOException e) {e.printStackTrace();}}return result;}/*** @param url eg:http://localohost:8080/web/user/1*            http://localohost:8080/web/user?id=1*            http://localohost:8080/web/user?id=1,2,3* @return*/public static String doDelete(String url) {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String result = null;try {// 创建Http Post请求HttpDelete httpDelete = new HttpDelete(url);// 执行http请求response = httpClient.execute(httpDelete);result = EntityUtils.toString(response.getEntity(), Encoder.UTF_8);} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpClient.close();} catch (IOException e) {e.printStackTrace();}}return result;}
}

HttpClients调用restful接口get,post,put,delete接口工具类相关推荐

  1. ImageView可直接调用的,根据URL设置图片的工具类

    ImageView 是Android编程中最常用的组件之一. 但是根据图片的URL设置图片却很麻烦.因为获取网络图片的操作必须在异步线程中进行,根据URL设置ImageView图片就可能需要进行线程间 ...

  2. 调用阿里云发送短信验证码的工具类

    /*** @Description 阿里云短信发送工具类* @Author 曰业而安*/ public class SendSmsUtil { //phone 手机号 //param 验证码 注意这里 ...

  3. 腾讯开放平台接口鉴权(计算签名)工具类 java版

    腾讯开放平台 接口鉴权(签名)工具类 java版 用到了Hutool工具类 package top.seasmall.platform.core.config.nettyws.util;import ...

  4. 三种方法实现调用Restful接口

    1.基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring ...

  5. java创建restful接口,三种方法实现java调用Restful接口

    转自:https://www.cnblogs.com/taozhiye/p/6704659.html 引言:当前,web开发中第三方接口的选择往往是在restful接口和web service 接口 ...

  6. java调用restful接口_Java调用RESTful接口的几种方式

    前端一般通过Ajax来调用,后端调用的方式还是挺多的,比如HttpURLConnection,HttpClient,Spring的RestTemplate 服务端代码如下: 服务端接口请求的URL:h ...

  7. php调用Restful API接口

    /*** [http 调用接口函数]* @param string $url [接口地址]* @param array $params [数组]* @param string $method [GET ...

  8. php调用restful接口_PHP restful 接口

    首先我们来认识下RESTful Restful是一种设计风格而不是标准,比如一个接口原本是这样的: http://www.test.com/user/view/id/1 表示获取id为1的用户信息,如 ...

  9. ajax对接接口属性,ajax调用restful接口

    ajax调用restful接口 内容精选 换一换 ROMA API提供了API服务能力,通过API服务把后端服务封装成标准RESTful格式API,并开放给其他用户使用.目前支持三种创建服务方式:创建 ...

  10. python restful风格_总结python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法

    python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法 这两天在用python的bottle框架开发后台管理系统,接口约定使用RESTful风格请求,前 ...

最新文章

  1. 阿里开源MNNKit:基于MNN的移动端深度学习SDK,支持安卓和iOS
  2. 扫描自己进入VR中 SLAM Scan 3D引擎将做到
  3. java标志清理_JVM内存管理之GC算法精解(五分钟让你彻底明白标记/清除算法)...
  4. RMQ问题-ST方法
  5. 文巾解题 1418. 点菜展示表
  6. ecc加解密算法 c++_ECC加密算法的基本介绍
  7. 找不到问题的核心,你永远解决不了问题。
  8. 编写代码的软件用什么编写的_如果您编写代码,这就是您的黄金时代
  9. ie和谷歌在java中空格兼容,谷歌和IE浏览器的兼容性问题,相同的html结构竟然在两个浏览器不一样...
  10. Spring简单的文件配置
  11. Missing artifact XXXXX:jar:1.9.1 解决错误问题
  12. [转]numpy 100道练习题
  13. 南非醉茄提取物行业调研报告 - 市场现状分析与发展前景预测
  14. 线程的常用方法(1)
  15. shell 执行多个python脚本
  16. AB工业计算机编程软件,罗克韦尔/AB VersaView 5100 工业监视器
  17. 很多人不理解这个参数的 nl,nh:ESC * m nL nH d1... dk
  18. 支付宝回调验证失败问题
  19. 关于阴阳师日和坊的快速刷出方法以及相关思考
  20. 免费生成!火爆全网的个人行程卡纪念版!

热门文章

  1. gitlab私服搭建
  2. Javascript中call()和apply()的用法 ----1
  3. javascript 多线程异步队列
  4. Adobe AIR简单的缓存技术
  5. VB 6.0使用api
  6. 【心情】换个皮肤试试看
  7. CentOS安装后初始配置
  8. Linux之http服务基本配置案例
  9. java - 常见对象object
  10. Debian安装autoconf