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;

      }

}

最新文章

  1. 架构师养成记--12.Concurrent工具类CyclicBarrier和CountDownLatch
  2. Qt实用小技巧(转)
  3. ASP.Net上传大文件解决方案之IIS7.0下的配置
  4. JavaScript Patterns 4.5 Immediate Functions
  5. Use a PowerShell Module to Easily Export Excel Data to CSV
  6. [dpdk] 读官方文档(2)
  7. vb 和vb.net的区别
  8. 用 Android-X86 和 VirtualBox 玩安卓游戏
  9. [转]分布式系统为什么需要 Tracing?
  10. UVALive 7148 LRIP 14年上海区域赛K题 树分治
  11. html——基础样式篇(1)
  12. UVa 103 - Stacking Boxes (LIS,打印路径)
  13. Array数组常用的5个方法
  14. 2017多校第10场 HDU 6180 Schedule 贪心,multiset
  15. 常用javascript表单验证方法
  16. shiro的登陆认证(shiro项目中来的一)
  17. 2-2、安装Filebeat
  18. percona-toolkit工具的使用
  19. python_day4
  20. 99乘法表的正反写 (python的写法)

热门文章

  1. 北风设计模式课程---单一职责原则(Single Responsibility Principle)
  2. Http Handler 介绍
  3. DP---DAG、背包、LIS、LCS
  4. 20190925 On Java8 第二十二章 枚举
  5. APM全链路监控--日志收集篇
  6. Flask搭建简单的get请求
  7. Win10不能远程其他远程计算机的解决办法
  8. 什么是 Java 对象深拷贝?面试必问!
  9. Python中yield和return两者之间区别
  10. 回溯--- Permutations