apache httpclient 的请求封装

package com.itemutils.http.apache;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

/**

  • @Author zbf
  • @DATA 2019/8/28 15:54
  • @ClassIntroduction 使用apache httpclient进行封装的请求
  • 基于apchehttpclient的封装
  • 需要使用的jar 包
  •       <dependency>
    
  •         <groupId>org.apache.httpcomponents</groupId>
    
  •         <artifactId>httpclient</artifactId>
    
  •         <version>4.5.6</version>
    
  •     </dependency>
    
  •       <dependency>
    
  •         <groupId>com.alibaba</groupId>
    
  •         <artifactId>fastjson</artifactId>
    
  •         <version>1.2.47</version>
    
  •     </dependency>
    

*/
public class UseHttp {

/**
* 请求类型
*/
JSON(“Content-Type”,“application/json”),
XML(“Content-Type”,“text/xml”),

/**
 * get请求 不设置请求头
 * @param url 请求地址
 * @return
 */
public static String get(String url){
    HttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = getAddHeader(url, null);
    try {
        HttpResponse execute = httpClient.execute(httpGet);
        return EntityUtils.toString(execute.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}


/**
 * get 请求
 * @param url 请求url
 * @param header 请求头参数
 * @return
 */
public static String get(String url,Map<String,String> header){
    HttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = getAddHeader(url, header);
    try {
        HttpResponse execute = httpClient.execute(httpGet);
        return EntityUtils.toString(execute.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

/**
 *
 * @param header 请求头信息
 */
private static HttpGet getAddHeader(String url ,Map<String, String> header){
    HttpGet httpGet = new HttpGet(url);
    if (null!=header && header.size()>0){
        header.entrySet().forEach(e->httpGet.addHeader(e.getKey(),e.getValue()));
    }else {
        httpGet.addHeader(HttpType.JSON.getName(),HttpType.JSON.getType());
    }
    return httpGet;
}


/**
 * post 请求 json格式
 * @param url 请求地址
 * @param header 请求头信息
 * @param body 请求体中信息
 * @return
 */
public static String post(String url,Map<String,String> header,Map<String,String> body){
    return post(url,header,body,null, null);
}


/**
 * post请求
 * @param url 请求地址
 * @param header 请求头中信息
 * @param dataXml 请求参数为txt or xml
 * @return
 */
public static String post(String url,Map<String,String> header,String dataXml){
    return post(url,header,null,null, dataXml);
}

/**
 * post 请求
 * @param url 请求连接
 * @param header 请求头的信息
 * @param body 请求体参数
 * @param bodyType 请求体的参数类型 默认是utf-8
 * @return
 */
public static String post(String url,Map<String,String> header,Map<String,String> body,String bodyType,String txtXml){
    HttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = postAddHeader(url,header);
    try {
        //设置请求参数的格式为json
        if (header.get(HttpType.JSON.getName()).equals(HttpType.JSON.getType())){
            httpPost.setEntity(new StringEntity(JSON.toJSONString(body),(bodyType==null ||"".equals(bodyType))?HttpConstant.UTF_8:bodyType));
        }
        //设置请求参数的类型格式xml
        else if (header.get(HttpType.XML.getName()).equals(HttpType.XML.getType())){
            httpPost.setEntity(new StringEntity(txtXml,(bodyType==null ||"".equals(bodyType))?HttpConstant.UTF_8:bodyType));
        }
        HttpResponse response = httpClient.execute(httpPost);
        return response.getEntity().toString();
    } catch (IOException e)  {
        e.printStackTrace();
    }
    return null;
}





/**
 * post请求头的封装
 * @param header 请求头信息
 */
private static HttpPost postAddHeader(String url ,Map<String, String> header){
     HttpPost httpPost = new HttpPost(url);
    if (null!=header && header.size()>0){
      header.entrySet().forEach(e->httpPost.addHeader(e.getKey(),e.getValue()));
    }else {
        httpPost.addHeader(HttpType.JSON.getName(),HttpType.JSON.getType());
    }
    return httpPost;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值