private String sendPost(Map<String,Object> data, String url) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuffer sb = new StringBuffer();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> valuePairs = new ArrayList<>();
if(null != data) {
for (String key : data.keySet()) {
if(data.get(key) != null){
valuePairs.add(new BasicNameValuePair(key, data.get(key)
.toString()));
}
}
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(valuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent());
byte [] buffer;
while (0<bis.read(buffer=new byte[128])){
sb.append(new String(buffer,"utf-8"));
}
}catch (UnsupportedEncodingException e){//数据格式有误
e.printStackTrace();
}catch (IOException e){//请求出错
e.printStackTrace();
}finally {
httpPost.releaseConnection();
}
return sb.toString();
} public String sendGet(String url) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuffer sb = new StringBuffer();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity();
InputStreamReader reader = new InputStreamReader(entity.getContent(), "utf-8");
char[] charbufer;
while (0 < reader.read(charbufer = new char[10])) {
sb.append(charbufer);
}
} catch (IOException e) {//
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return sb.toString();
}

HttpClient 实现 get,post请求的更多相关文章

  1. HttpClient (POST GET PUT)请求

    HttpClient (POST GET PUT)请求 package com.curender.web.server.http; import java.io.IOException; import ...

  2. HttpClient方式模拟http请求设置头

    关于HttpClient方式模拟http请求,请求头以及其他参数的设置. 本文就暂时不给栗子了,当作简版参考手册吧. 发送请求是设置请求头:header HttpClient httpClient = ...

  3. HttpClient发送get post请求和数据解析

    最近在跟app对接的时候有个业务是微信登录,在这里记录的不是如何一步步操作第三方的,因为是跟app对接,所以一部分代码不是由我写,我只负责处理数据,但是整个微信第三方的流程大致都差不多,app端说要传 ...

  4. HttpWebRequest 改为 HttpClient 踩坑记-请求头设置

    HttpWebRequest 改为 HttpClient 踩坑记-请求头设置 Intro 这两天改了一个项目,原来的项目是.net framework 项目,里面处理 HTTP 请求使用的是 WebR ...

  5. 使用HttpClient发送Get/Post请求 你get了吗?

    HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议 ...

  6. org.apache.httpcomponents httpclient 发起HTTP JSON请求

    1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...

  7. httpclient的几种请求URL的方式

    一.httpclient项目有两种使用方式.一种是commons项目,这一个就只更新到3.1版本了.现在挪到了HttpComponents子项目下了,这里重点讲解HttpComponents下面的ht ...

  8. HttpClient发起Http/Https请求工具类

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

  9. HttpClient方式模拟http请求

    方式一:HttpClient import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.http.*; im ...

  10. Android HttpClient GET或者POST请求基本使用方法(转)

    在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpCl ...

随机推荐

  1. jQuery兼容浏览器IE8方法

    在维护公司网站的时候,发现在IE8下jquery会报对象不支持此属性或方法.缺少对象的错误:  在其他浏览器就可以正常运行,当前使用的jquery版本是3.1.1,查资料发现jquery从2.0开始不 ...

  2. api-gateway实践(07)新服务网关 - 手动发布

    应用地址:http://10.110.20.191:8080/api-gateway-engine/ 一.准备工作 1.xshell登陆云主机 1.1.配置链接 1.2.链接成功 1.3.关闭防火墙 ...

  3. Linux知识积累(5) 关机shutdown和重启reboot

    Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 1 ...

  4. ssh_maven之controller层开发

    我们已经完成了前两层的开发,现在 只剩下我们的controller层了,对于这一层,我们需要创建一个动作类CustomerAction,另外就是我们的strutss.xml以及我们的applicati ...

  5. Python之面向对象三

    面向对象的三大特性: 多态 多态指的是一类事物有多种形态.Python3天生支持多态. 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCM ...

  6. MYSQL 面试查询系列问题

    表结构: `student`('id'.'name'.'code'.'age'.'sex')学生表 `teacher`('id'.'name')教师表 `course`('id'.'name'.'te ...

  7. html标记语言 --表单

    html标记语言 --表单 七.表单 1.表单标记 1.1表单中的内容 <form></form>定义表单的开始位置和结束位置,表单提交时的内容就是<form>表单 ...

  8. with工作原理

    进入时,调用对象的__enter__ 退出时,调用对象的__exit__

  9. 【转】C++ Vector(向量容器)

    转自:https://blog.csdn.net/studentyyl/article/details/21177445 vector是一个线性顺序结构.相当于数组,但其大小可以不预先指定,并且自动扩 ...

  10. 【linux之crontab,启动】

    一.计划任务 atd at命令发布的任务计划 一次性的任务计划 at time ctrl+d 提交 time: 1.绝对时间:12:00 2.相对时间:+8 3.模糊时间:noon midnight ...