HttpClientUtil工具类封装
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工具类封装的更多相关文章
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- (转载) 百度地图工具类封装(包括定位,附近、城市、范围poi检索,反地理编码)
目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选 程序员8月书讯 项目管理+代码托管+文档协作,开发更流畅 百度地图工具类封装(包括定位,附近.城市.范围poi检索, ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战
笔记 4.Redis工具类封装讲解和实战 简介:高效开发方式 Redis工具类封装讲解和实战 1.常用客户端 https://redisdesktop.com/download ...
- flink---实时项目--day02-----1. 解析参数工具类 2. Flink工具类封装 3. 日志采集架构图 4. 测流输出 5. 将kafka中数据写入HDFS 6 KafkaProducer的使用 7 练习
1. 解析参数工具类(ParameterTool) 该类提供了从不同数据源读取和解析程序参数的简单实用方法,其解析args时,只能支持单只参数. 用来解析main方法传入参数的工具类 public c ...
- 关于TornadoFx和Android的全局配置工具类封装实现及思路解析
原文地址: 关于TornadoFx和Android的全局配置工具类封装实现及思路解析 - Stars-One的杂货小窝 目前个人开发软件存在设置页面,可以让用户自定义些设置,但我发现,存储数据的代码逻 ...
- Android Sqlite 工具类封装
鉴于经常使用 Sqlite 数据库做数据持久化处理,进行了一点封装,方便使用. 该封装类主要支持一下功能 支持多用户数据储存 支持 Sqlite数据库升级 支持传入 Sql 语句建表 支持 SQLit ...
随机推荐
- fengmiantu3
- ijkplayer阅读笔记系列<转>
http://blog.csdn.net/peckjerry/article/details/47663275
- Linux内核调试方法总结之内核通知链
Linux内核通知链notifier 1.内核通知链表简介(引用网络资料) 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...
- c# html 转Word--Spire.Doc
利用 Spire.Doc 组件,NuGet搜索“FreeSpire.Doc”有个免费版. using System;using System.Collections.Generic;using Sys ...
- fedora禁用(开机启动)服务和进程管理
首先要查看有哪些(开机启动)服务 chkconfig --list 或者: systemctl list-units 然后, 根据需要进行禁用服务的开机启动: chkconfig service_na ...
- django框架ORM数据库
字段类型 选项 null是数据库范畴的概念,blank是表单验证范畴的 外键 在设置外键时,需要通过on_delete选项指明主表删除数据时,对于外键引用表数据如何处理,在django.db.mode ...
- flutter dialog异常Another exception was thrown: No MaterialLocalizations found
flutter dialog异常Another exception was thrown: No MaterialLocalizations found import 'package:flutter ...
- Postman + Newman 生成测试报告
1.安装Node.js 下载地址: https://nodejs.org/download/ 2.安装Newman 1) 打开cmd,输入:npm install -g newman 2) 安装支持N ...
- PHP 的源码编译安装
PHP 架构和安装扩展的几种方式 PHP 三大模块: SAPI:接受并处理请求. Zend Engine:PHP 的核心,负责分析 PHP 代码并转为 opcode,然后在 Zend VM 虚拟机上执 ...
- 安装element-ui
element地址:https://element.eleme.cn/2.0/#/zh-CN/component/quickstart 1.在新建终端 [安装element-ui组件依赖]cnpm i ...