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 使用案例
猜你喜欢
转载自www.cnblogs.com/MagicAsa/p/11597639.html
今日推荐
周排行