HttpClient 使用案例
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 使用案例的更多相关文章
- HttpClient 应用案例揭破应用Discuss论坛登录
闲来无事,写了一个对discuss论坛登录的案例,初次上场按照以前的惯例没成功,见过抓包分析discuss论坛成功完成,废话不多说 直接上代码. 1:winform 做客户端,添加HttpClient ...
- httpclient的理解(代码理解)
一,httpclient的理解 httpcliet就是模仿浏览器在服务器内部从一个项目调用另一个项目的技术.比如说调用接口等. HttpClient 是 Apache Jakarta Common ...
- SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例
要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...
- 07_android入门_採用HttpClient的POST方式、GET方式分别实现登陆案例
1.简单介绍 HttpClient 是 Apache Jakarta Common 下的子项目,能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,而且它支持 HTTP 协议 ...
- Android(java)学习笔记211:采用httpclient提交数据(qq登录案例)
1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包 ...
- httpclient案例二(调用百度地图的接口)
一,class T package com.http.test; import org.junit.Test; import com.http.BaiduMapProxyService; import ...
- httpclient案例一(调用识别接口)
public Map<String, Object> pictureRecognition(String recotype, MultipartFile imageFile) { Stri ...
- Android(java)学习笔记154:采用HttpClient提交数据(qq登录案例)
1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包 ...
- HttpClient方式调用接口的java 简单案例源码+附jar包
1 package com.itNoob.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache. ...
随机推荐
- 关于虚拟机中linux系统时间的问题
由于考试需求,我在vm上放置了考试用的linux环境,在进行操作的时候需要回调时间才能进行一些操作.但是每次重启之后,时间总是会和物理服务器的时间进行同步,让我非常的苦恼. 终于有一天我想清楚了如何表 ...
- Vultr账户充值需要注意的事项
一.Vultr充值注意事项 1.选择适合自己的充值工具.我们可以使用支付宝.信用卡.PAYPAL等支付方式,需要我们注意的是充值方式不要来回切换充值,如果我们有多账户的时候一定不要相互使用,以免导致账 ...
- openwrt redis
2071 make V=s 2072 cd build_dir/target-x86_64_uClibc-0.9.33.2/root-x86/ cd package/network/services/ ...
- web 开发流程
shopWeb登录 开发步骤 1 数据库 2 创建 Module 3 复制页面 4 创建目录包 添加需要的jar包(引入依赖) 配置文件 5 功能: 编写服务器程序
- Java中的线程Thread方法之---join()
上一篇我们说到了Thread中的stop方法,这一篇我们再来看一下方法join的使用,那么方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答. join方法从字面上的意 ...
- NOIp2018集训test-9-17(am)
这是一套去年在长沙考过的题,但是我当时就没理清楚+没写题解(我以前很多博客都写得跟shi一样,完全没有意义,看到就想打当时的我),所以又考得稀烂. T1.star way to heaven 容易想到 ...
- NX二次开发-UFUN由工程图视图tag获取图纸页tag UF_DRAW_ask_drawing_of_view
#include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...
- NX二次开发-UFUN创建圆锥UF_MODL_create_cone1
NX11+VS2013 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建圆锥 UF_FEATURE_SIGN ...
- IP总结
网络层向上只提供无连接的.尽最大努力支付的数据报服务 IP地址,32位,分为两部分,网络和主机标示 IP地址分类: A类:0开头,1-8位为网络标示 B类:10开头,1-16位为网络标示 C类:110 ...
- Api:目录
ylbtech-Api:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.c ...