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 ...
随机推荐
- animate(动画)框架 和 swiper (轮播)框架 的使用
swiper.js 框架 网址:https://www.swiper.com.cn/ 是一个专门做轮播,切换特效的轮播 使用方法: 然后进入案例,通过案例来进行各种功能的实现, 这一步是教我们怎么做, ...
- 查询数据库对象的DDL信息
表的DDL select dbms_metadata.get_ddl('HBBL','TABLE','AGREEMENT_MIRROR') from dual:
- KindEditor上传图片一直提示undefined
图片已经上传成功了,但是就是不在文本编辑器里显示图片,一直弹出undefined 返回的JSON都对呀!这是官网说的返回值: //成功时 { "error" : 0, " ...
- 搭建python-flask开发环境
ubuntu环境 1. 更新系统软件源: 没有通过更新系统软件源的话,可能无法通过apt-get install安装我们需要用到的软件: $ sudo apt-get update $ sudo ap ...
- Oracle-数据导出和导入
对数据库进行逻辑备份或数据转储,也就是对数据库实时导入.导出操作时,既可以使用常规的EXP/IMP客户端程序,也可以使用数据泵技术:IMPDP/EXPDP 使用数据泵导出或导入的优点: 1.数据泵导出 ...
- spring boot 整合 RabbitMQ 错误
1.错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spr ...
- python接口自动化:https请求,取消警告
实现代码如下: import requests r=requests.get('https://www.baidu.com',verify=False) rr=r.content.decode() p ...
- SpringCloud启动Eureka server时报错 java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not present
SpringBoot打开Eureka server时出现以下错误: java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext ...
- 最长k可重线段集问题【费用流】【优先队列Dijkstra费用流】
题目链接 与最长K可重区间问题一样的解法,但是这道题却有很多需要注意的地方,譬如就是精度问题,一开始没考虑到sprt()里面的乘会爆了精度,然后交上去竟然是TLE,然后找的原因方向也没对,最后TLE了 ...
- c#工厂模式
创建一个抽象类: public abstract class Test { public abstract void Print();//输出信息} 创建输出123的测试类 public class ...