/**
* 发送post请求
* @param URL 数据发送地址
* @param json json格式数据内容
* @param headParams 请求头内容
* @return 请求结果
*/
public static String sendPost(String URL, Object json, HashMap<String,String> headParams) throws IOException {
String result = "";
CloseableHttpClient client = HttpClients.createDefault();
try {
HttpPost post = new HttpPost(URL);
post.setHeader("Content-Type", "application/json");
//增加请求头参数
headParams.forEach((key,value) -> {
post.addHeader(key, value);
}); StringEntity s = new StringEntity(json.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(s);
//构建超时等配置信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(60*1000) //连接超时时间
.setConnectionRequestTimeout(60*1000) //从连接池中取的连接的最长时间
.setSocketTimeout(60 * 1000) //数据传输的超时时间
.build();
post.setConfig(config);
CloseableHttpResponse response = client.execute(post);
try {
HttpEntity resEntity = response.getEntity();
//回复接收
result = EntityUtils.toString(resEntity, "UTF-8");
} finally {
response.close();
}
} catch (Exception e) {
throw e;
}finally {
client.close();
}
return result;
}

  

最新文章

  1. 图解HTTP
  2. [.net 面向对象程序设计深入](6).NET MVC 6 —— 模型、视图、控制器、路由等的基本操作
  3. 解决Eclipse项目图标上的红色感叹号,红叉
  4. STM32 CANBus RAM Layout
  5. 写给程序员和UI--Android的切图标准
  6. Android 学习笔记之Volley开源框架解析(三)
  7. Android 自定义列表指示器
  8. 欧拉工程第69题:Totient maximum
  9. C语言结构体的强制类型转换
  10. oracle excute immediate 单引号转义
  11. Spring MVC Controller 单元测试
  12. php中文乱码问题分析及解决办法
  13. Papers3
  14. 【原创】大叔经验分享(12)如何程序化kill提交到spark thrift上的sql
  15. 论文笔记:Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
  16. 获取docker容器的ip地址
  17. keras LSTM学习实例
  18. LVS 实现负载均衡原理及安装配置详解
  19. JDBC规范(转)
  20. Android中 单位 介绍

热门文章

  1. Activity 和 Fragment 中的视图绑定
  2. LeetCode-794 有效的井字游戏
  3. MySQL联合索引的创建规则
  4. Windows打开代理快捷方式
  5. 【python操作Excel的常见方法汇总】 xlrd pandas xlwings
  6. SDN实验环境安装
  7. finalshell如何查看密码
  8. Qt中父子页面切换隐藏实现方法 (利用信号槽机制实现)
  9. 实现两个APP之间的跳转传值
  10. 如何用python将txt中的package批量安装