package com.jd.ng.shiro.utils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpStatus;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

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;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

/**

  • @Author: husToy.Wang

  • @Date: 2019/8/5 9:38

  • @Version 1.0

    */

    public class HttpClientUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    /**

    • 封装 doGet方法,执行

    • @param url

    • @return

      */

      public static String doGet(String url) {

      CloseableHttpClient httpClient = HttpClients.createDefault();

      HttpGet httpGet = new HttpGet(url);

      /**

      • 设置请求头

        */

        //httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));

      CloseableHttpResponse response = null;

      try {

      response = httpClient.execute(httpGet);

       if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); httpClient.close(); return result;
      }
      httpClient.close();

      } catch (IOException e) {

      logger.error("httpClient.doGet.error:{}", e.getMessage());

      return null;

      }

      return null;

      }

    /**

    • 封装 post请求

    • @return

      */

      public static String doPost(String url) throws IOException {

      String result = null;

      CloseableHttpClient httpClient = HttpClients.createDefault();

      CloseableHttpResponse response = null;

      try {

       HttpPost httpPost = new HttpPost(url);
      
       // 定义请求的配置
      RequestConfig config = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(2000).build();
      httpPost.setConfig(config); List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
      parameters.add(new BasicNameValuePair("scope", "project"));
      parameters.add(new BasicNameValuePair("q", "java")); // 提交参数发送请求
      UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(parameters); // 请求中设置请求体
      httpPost.setEntity(urlEncodedFormEntity); // 如果需要,设置请求头,多个请求头
      //Header[] headers = {new BasicHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")};
      //httpPost.setHeaders(headers); response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity);
      httpClient.close();
      return result;
      }

      } catch (Exception e) {

      logger.error("httpClient.doPost.error:{}", e.getMessage());

      return null;

      }

      return null;

      }

}

HttpClientUtil工具类封装的更多相关文章

  1. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  2. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  3. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  4. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  5. (转载) 百度地图工具类封装(包括定位,附近、城市、范围poi检索,反地理编码)

    目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选      程序员8月书讯      项目管理+代码托管+文档协作,开发更流畅 百度地图工具类封装(包括定位,附近.城市.范围poi检索, ...

  6. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战

    笔记 4.Redis工具类封装讲解和实战     简介:高效开发方式 Redis工具类封装讲解和实战         1.常用客户端 https://redisdesktop.com/download ...

  7. flink---实时项目--day02-----1. 解析参数工具类 2. Flink工具类封装 3. 日志采集架构图 4. 测流输出 5. 将kafka中数据写入HDFS 6 KafkaProducer的使用 7 练习

    1. 解析参数工具类(ParameterTool) 该类提供了从不同数据源读取和解析程序参数的简单实用方法,其解析args时,只能支持单只参数. 用来解析main方法传入参数的工具类 public c ...

  8. 关于TornadoFx和Android的全局配置工具类封装实现及思路解析

    原文地址: 关于TornadoFx和Android的全局配置工具类封装实现及思路解析 - Stars-One的杂货小窝 目前个人开发软件存在设置页面,可以让用户自定义些设置,但我发现,存储数据的代码逻 ...

  9. Android Sqlite 工具类封装

    鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...

随机推荐

  1. 北风设计模式课程---接口分离原则(Interface Segregation Principle)

    北风设计模式课程---接口分离原则(Interface Segregation Principle) 一.总结 一句话总结: 接口分离原则描述为 "客户类不应被强迫依赖那些它们不需要的接口& ...

  2. Android操作系统中11种传感器的介绍【转】

    本文转载自:http://www.oschina.net/question/163910_28354 在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. ...

  3. P1439 【模板】最长公共子序列(LCS)

    先来看一看普通的最长公共子序列 给定字符串A和B,求他们的最长公共子序列 DP做法: 设f[i][j]表示A[1~i]和B[1~j]的最长公共子序列的长度 那么f[i][j]=max(f[i-1][j ...

  4. 关于问题的四个单词区别: question problem matter issue

    [[ 网上讨论的 problem, question, issue, matter这些名词均含"问题"之意.problem: 指客观上存在的.难以处理或难以理解的问题.questi ...

  5. tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

    在执行自动化打包的时候报错,检查发现是Xcode的路径被改了 标记3的地方原来默认是没有内容的,点击它,然后会自动弹出一个选项,就是xcode的版本. 修改后,在命令行输入xcodebuild命令测试 ...

  6. VMware 虚拟化编程(1) — VMDK/VDDK/VixDiskLib/VADP 概念简析

    目录 目录 VMDK VDDK VixDiskLib VADP VMDK VMDK(VMware's Virtual Machine Disk Format,VMware 虚拟磁盘格式):简单来说就是 ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_10 打印流_1_打印流_概述和使用

    system.out就是打印流 System的源码里面out参数就是一个PrintStream类型的 例如路径写的是E盘,.没有e盘这个路径就会抛出异常 用println直接写入的就是97    这个 ...

  8. centos 6.5 升级openssh-7.5

    1.环境 2.安装telnet 服务,防止ssh升级之后登陆不上服务器,使用telnet 连接服务器 yum install telnet-server -y chkconfig telnet on ...

  9. springboot An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]

    1.错误 An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, wh ...

  10. jmeter逻辑控制器详解(2)

    逻辑控制器 8.Runtime Controller 运行周期控制器,顾名思义,这是一种设置运行时间的控制器,它的效果就是使该控制器下的子项运行时间为[Runtime]中的数值(单位:s). Runt ...