1.maven依赖

<dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.2</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4</version>
</dependency>

2.HttpUtils(POST/GET)

public final class HttpUtils {private HttpUtils() {}/*** contentType: application/json*/public static String post(String address, Map<String, String> headers, String body) {StringBuilder sb = new StringBuilder();try {CloseableHttpClient httpclient = HttpClientBuilder.create().build();HttpPost httpPost = new HttpPost(address);if (StringUtils.isNotBlank(body)) {StringEntity stringEntity = new StringEntity(body);stringEntity.setContentEncoding("UTF-8");stringEntity.setContentType("application/json");httpPost.setEntity(stringEntity);}if (MapUtils.isNotEmpty(headers)) {setHeaders(httpPost, headers);}HttpResponse response = httpclient.execute(httpPost);if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){sb.append(EntityUtils.toString(response.getEntity()));}httpPost.releaseConnection();} catch (Exception e) {throw new RuntimeException("网络请求失败");}return sb.toString();}/*** contentType: key/value*/public static String post(String address, Map<String, String> headers,  Map<String, String> paramsMap) {StringBuilder sb = new StringBuilder();try {CloseableHttpClient httpclient = HttpClientBuilder.create().build();HttpPost httpPost = new HttpPost(StringUtils.trim(address));if (MapUtils.isNotEmpty(paramsMap)) {List<NameValuePair> params = getParams(paramsMap);httpPost.setEntity(new UrlEncodedFormEntity(params));}if (MapUtils.isNotEmpty(headers)) {setHeaders(httpPost, headers);}CloseableHttpResponse response = httpclient.execute(httpPost);if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){sb.append(EntityUtils.toString(response.getEntity(), "utf-8"));}httpPost.releaseConnection();} catch (Exception e) {e.printStackTrace();}return sb.toString();}/*** GET (INCLUDE HEADERS)*/public static String get(String address, Map<String, String> headers) {StringBuilder sb = new StringBuilder();try {CloseableHttpClient httpclient = HttpClientBuilder.create().build();HttpGet httpGet = new HttpGet(StringUtils.trim(address));if (MapUtils.isNotEmpty(headers)) {setHeaders(httpGet, headers);}CloseableHttpResponse response = httpclient.execute(httpGet);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {sb.append(EntityUtils.toString(response.getEntity(), "utf-8"));}httpGet.releaseConnection();} catch (Exception e) {e.printStackTrace();}return sb.toString();}private static void setHeaders(HttpRequestBase request, Map<String, String> headers) {for (Map.Entry<String, String> entry : headers.entrySet()) {request.setHeader(entry.getKey(), entry.getValue());}}private static List<NameValuePair> getParams(Map<String, String> paramsMap) {List<NameValuePair> params = new ArrayList<>();for (String key : paramsMap.keySet()) {params.add(new BasicNameValuePair(key, paramsMap.get(key)));}return params;}
}

HttpUtils(POST/GET)相关推荐

  1. 基于HttpClient的HttpUtils(后台访问URL)

    最近做在线支付时遇到需要以后台方式访问URL并获取其返回的数据的问题,在网络上g了一把,发现在常用的还是Apache的HttpClient.因为以经常要用到的原故,因此我对其进行了一些简单的封装,在此 ...

  2. Httputils请求网络数据

    private void getData(final String s) { i++;         // 请求网络数据         HttpUtils utils = new HttpUtil ...

  3. java模拟浏览器请求HttpUtils,可秒杀京东优惠券

    2019独角兽企业重金招聘Python工程师标准>>> package com.boot.utils;import java.io.BufferedReader; import ja ...

  4. 安卓day29网络编程 HttpClient AsyncHttpClient 断点续传多线程下载器 HttpUtils

    一.排坑 HttpClient.Header飘红 Android 6.0 已经移除了httpClient module下的build.gradle中加入: android{useLibrary 'or ...

  5. Java 常用HTTP请求工具类HttpUtils

    .pom依赖 <!-- httpclient --><dependency><groupId>org.apache.httpcomponents</group ...

  6. 超好用的后端发送http请求HttpUtils工具类(基于原生http连接,不需要另外导包)

    在项目中,为了实现一些特定的功能,我们常常需要发送http异步请求 ,为此需要特意封装一个实用的HttpUtils工具类 HttpUtils工具类内容如下: package com.zyw.secki ...

  7. httpUtils网络请求

    String path = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";HttpUtils httpUtils = new HttpUtils( ...

  8. android httputils更换成https请求,Android开发工具类之HttpUtils

    今天我们讲常用的开发工具类之HttpUtils,我发现上两次,我对于每个方法都进行了一定的解释,有人跟我评论和留言说,不用我解释,这么简单,这么明显的使用方法,再笨的人也能看懂,多此一举,好吧,这次我 ...

  9. XUtils之HttpUtils

    HttpUtils请求网络数据 import java.util.List; import com.example.week1_httputils.Car.Carmsg; import com.goo ...

最新文章

  1. HashMap 的长度为什么是 2 的幂次方?
  2. n个素数构成等差数列
  3. 微软开发中心的rss历史记录(24)
  4. Google Maps API编程资源大全
  5. Nacos client SDK 订阅式请求坑
  6. 【英语学习】【WOTD】leviathan 释义/词源/示例
  7. Airflow 中文文档:使用操作器
  8. LeetCode 775. Global and Local Inversions
  9. 如何高效阅读 Spark 和 Hadoop 这类大型开源项目源代码?
  10. 批量删除redis key
  11. amazeui学习笔记--css(常用组件2)--面包屑导航Breadcrumb
  12. CarMaker中关于交通目标行人横穿的问题
  13. 微信打印实现过程和免费下载
  14. 学习笔记(13):MATLAB基础入门课程-kron函数
  15. 【数理知识】狄利克雷函数 dirac(t)
  16. 文献阅读—A detection algorithm for cherry fruits based on the improved YOLO-v4 model
  17. Dubbo—— 一个服务既是消费者又是提供者
  18. Word 2019 自带公式快速编号方法
  19. 英语新闻app——TagLayout+ViewPager+Fragment实现分类切页功能
  20. aircrack-ng for windows的简单使用教程

热门文章

  1. oracle数据库中关于部门工资前三高的查询(重点学习思路)
  2. 全新qu水印微信小程序源码
  3. Daily-Interview-Question
  4. CAD图纸文件中灰色部分图像不能编辑怎么办?
  5. DOM编程-获取文本框的value
  6. 逆向工程实验_pre1(密码学算法破解)
  7. 计算机网络Cisco Packet Tracer仿真实操
  8. ajax进入error函数,报NetworkError: A network error occurred.
  9. linux安装svn
  10. 你真的了解Python的字符串吗?