package com.qifeng.config.ygx.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; /**
* @author zhao xin
* @Title: HttpClientUtil
* @ProjectName as_configurer
* @Description: TODO
* @date 2019/9/21 13:51
*/
public class HttpClientUtil { //同步get方式的请求 其中 uri 是请求的地址如:http://www.baidu.com
//主要http不能省略,否则会报 没有指明协议 的错误 如果需要带数据 则以uri?a=sss 形式即可
public void doGet() throws ClientProtocolException, IOException {
//创建CloseableHttpClient
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
//执行
HttpUriRequest httpGet = new HttpGet("uri");
CloseableHttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity!=null){
String entityStr= EntityUtils.toString(entity,"utf-8");
System.out.println(entityStr);
}
// System.out.println(response.toString());
} //同步post请求方式: 请求中需要带的数据通过
//
// httpPost.setEntity(new StringEntity("beppe", "UTF-8"));的方式,
//如果需要带的数据是对象的形式,则转化为json字符串格式
public void doPost() throws ClientProtocolException, IOException{
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
HttpPost httpPost= new HttpPost("uri");
httpPost.setEntity(new StringEntity("beppe", "UTF-8"));
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity!=null){
String entityStr= EntityUtils.toString(entity,"utf-8");
System.out.println(entityStr);
}
// System.out.println(response.toString());
} //异步get请求:
public void doGetAsyn() throws InterruptedException, ExecutionException {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
//开启httpclient
httpclient.start();
//开始执行
HttpGet httpGet = new HttpGet("uri");
Future<HttpResponse> future = httpclient.execute(httpGet, null);
HttpResponse httpResponse = future.get();
System.out.println(httpResponse.getStatusLine()+"==="+httpGet.getRequestLine());
} //异步的post方式请求:其中可以在回调函数中加入自己的业务逻辑
public static void doPostAsyn(String url,String outStr) throws ParseException, IOException, InterruptedException, ExecutionException{
CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault();
httpAsyncClient.start();
HttpPost httpost = new HttpPost(url);
// httpost.addHeader(HTTP.CONTENT_TYPE, "application/json");
StringEntity se=new StringEntity(outStr,"UTF-8");
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpost.setEntity(se);
Future<HttpResponse> future = httpAsyncClient.execute(httpost,null);
System.out.println(future.get().toString());
//String result = EntityUtils.toString(response.getEntity(),"UTF-8");
//jsonObject = JSONObject.fromObject(result);
} @SneakyThrows
public static String DoPost(String url, JSONObject params){
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
HttpPost httpPost= new HttpPost(url); List<BasicNameValuePair> list=new ArrayList<>(); if(params!=null) {
if(params.get("Content-Type")!=null){
httpPost.addHeader("Content-Type", params.get("Content-Type").toString());
}
if(params.get("UserName")!=null){
httpPost.addHeader("UserName", params.get("UserName").toString()); }
if(params.get("type")!=null){
JSONObject Json = new JSONObject();
Json.put("type", params.get("type").toString());
StringEntity entity = new StringEntity(Json.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
}
if(params.get("stationId")!=null){
list.add(new BasicNameValuePair("stationId", params.get("stationId").toString()));
}
} if(params.get("Content-Type")==null) {
httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
}
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity!=null){
String entityStr= EntityUtils.toString(entity,"utf-8");
return entityStr;
}else{
return null;
}
} }

HttpClient 使用案例的更多相关文章

  1. HttpClient 应用案例揭破应用Discuss论坛登录

    闲来无事,写了一个对discuss论坛登录的案例,初次上场按照以前的惯例没成功,见过抓包分析discuss论坛成功完成,废话不多说 直接上代码. 1:winform 做客户端,添加HttpClient ...

  2. httpclient的理解(代码理解)

    一,httpclient的理解  httpcliet就是模仿浏览器在服务器内部从一个项目调用另一个项目的技术.比如说调用接口等. HttpClient 是 Apache Jakarta Common ...

  3. SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例

     要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...

  4. 07_android入门_採用HttpClient的POST方式、GET方式分别实现登陆案例

    1.简单介绍 HttpClient 是 Apache Jakarta Common 下的子项目,能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,而且它支持 HTTP 协议 ...

  5. Android(java)学习笔记211:采用httpclient提交数据(qq登录案例)

    1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包 ...

  6. httpclient案例二(调用百度地图的接口)

    一,class T package com.http.test; import org.junit.Test; import com.http.BaiduMapProxyService; import ...

  7. httpclient案例一(调用识别接口)

    public Map<String, Object> pictureRecognition(String recotype, MultipartFile imageFile) { Stri ...

  8. Android(java)学习笔记154:采用HttpClient提交数据(qq登录案例)

    1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包 ...

  9. HttpClient方式调用接口的java 简单案例源码+附jar包

    1 package com.itNoob.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache. ...

随机推荐

  1. 笔记58 Spring+Hibernate整合(一)

    Spring+Hibernate整合 一.整合思路 使DAO继承HibernateTemplate这个类 HibernateTemplate这个类提供了setSessionFactory()方法用于注 ...

  2. JAVA call dll

    { System.loadLibrary():装载Windows\System32下或jre\bin或Tomcat\bin目录下的本地链接库 System.load():根据具体的目录来加截本地链接库 ...

  3. [转]Delphi DLL的创建、静态 以及动态调用

    第一章  DLL简单介绍 由于在目前的学习工作中,需要用到DLL文件,就学习了下,在这里作个总结. 首先装简单介绍下DLL: 1,减小可执行文件的大小 DLL技术的产生有很大一部分原因是为了减小可执行 ...

  4. 获取ThinkPHP

    获取ThinkPHP的方式很多,官方网站(http://thinkphp.cn)是最好的下载和文档获取来源. 官网提供了稳定版本的下载:http://thinkphp.cn/down/framewor ...

  5. 网站数据采集|埋点设计|nginx日志文件

    数据获取的方式主要可以分为两种: 1.网站日志文件(log files) 页面埋点js自定义的采集. 优缺点: web服务器自带的日志记录功能:优点方便,缺点信息收集不全 自定义的js埋点收集:优点想 ...

  6. NX二次开发-UFUN遍历函数UF_OBJ_cycle_objs_in_part

    NX11+VS2013 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include < ...

  7. 使用Socket.IO做单页SPA应用更新

    单页应用的挑战之一是确保客户端软件和服务器应用相匹配. 举例:如果一个用Bobbie在他的浏览器中加载我们的单页应用,五分钟之后我们更新了服务器应用.现在Bobbiede遇到了问题,因为我们对服务器做 ...

  8. python之tkinter学习目录

    前言 下面的目录结构,采用的学习视频资料是网易云课堂中[莫凡]老师的,在目录的最下面的地方给出了对应的链接! 学习是逐渐积累起来的,代码也是!下面的每一篇中的对应代码,都秉承着这样的一个理念:代码是成 ...

  9. MDK KEIL 机构体初始化 . 点 成员 初始化, C99

    C99介绍,参考这里:C89,C99: C数组&结构体&联合体快速初始化 MDK 设置: 只需添加 ”--c99"参数即可,如图:

  10. RCC, Reset and Clock Control