基于httpclient封装的常用请求方式工具类

本文介绍了如何基于HTTPClient库封装常用的HTTP请求方法,通过这个工具类,可以便捷地进行GET、POST等操作,实现系统间接口调用和数据交互。
摘要由CSDN通过智能技术生成

本文偏重使用,简单讲述httpclient,httpclient其实就是模拟浏览器发起想服务器端的请求,而这种更加类似于JS的请求或页面的POST、GET,来完成系统间的接口调用,方便做其他的交互,本文是对httpclient进行二次封装完成的常用方法工具包:

package com.jusfoun.util.http;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

/** 
* @ClassName: HttpUtilImpl 
* @Description: HttpClinet请求工具包
* @author liuheming 
* @date 2017年2月1日 下午12:40:16 
*  
*/
@Service
public class HttpUtilImpl implements HttpUtil {

    @Override
    public String get(String uri, String chatset) {
        HttpClientBuilder create = HttpClientBuilder.create();
        HttpClient client = create.build();
        RequestConfig requestConfig = getRequestConfig();
        HttpGet httpGet = new HttpGet(uri);
        httpGet.setConfig(requestConfig);
        try {
            HttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            byte[] contentBytes = EntityUtils.toByteArray(entity);
            return new String(contentBytes, chatset);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpGet.abort();
            httpGet.releaseConnection();
        }
        return null;
    }

    @Override
    public String post(String uri, String chatset, String jsonParameters) {
        HttpClientBuilder create = HttpClientBuilder.create();
        HttpClient client = create.build();
        RequestConfig requestConfig = getRequestConfig();
        HttpPost post = new HttpPost(uri);
        post.setConfig(requestConfig);
        try {
            StringEntity stringEntity = new StringEntity(jsonParameters, chatset);
            post.setEntity(stringEntity);
            HttpResponse httpResponse = client.execute(post);
            HttpEntity responseEntity = httpResponse.getEntity();
            byte[] contentBytes = EntityUtils.toByteArray(responseEntity);
            return new String(contentBytes, chatset);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.abort();
            post.releaseConnection();
        }
        return null;
    }

    private RequestConfig getRequestConfig() {
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
        return requestConfig;
    }

    @Override
    public String post(String uri, String chatset, File file,String mediaType) {
        HttpClientBuilder create = HttpClientBuilder.create();
        HttpClient client = create.build();
        RequestConfig requestConfig = getRequestConfig();
        HttpPost post = new HttpPost(uri);
        post.setConfig(requestConfig);
        try {
            FileBody bin = new FileBody(file,ContentType.create(mediaType));
            MultipartEntityBuilder mpEntityBuilder = MultipartEntityBuilder.create(); // 文件传输
            mpEntityBuilder.addPart("media", bin);
            mpEntityBuilder.addTextBody("description", "{\"title\":\"title\",\"introduction\":\"introduction\"}");
            HttpEntity httpEntity = mpEntityBuilder.build();
            System.out.println(httpEntity.getClass());
            System.out.println("contentType : " + httpEntity.getContentType());
            System.out.println("contentLength : " + httpEntity.getContentLength());
            System.out.println("encoding : " + httpEntity.getContentEncoding());
            post.setEntity(httpEntity);
            HttpResponse httpResponse = client.execute(post);
            HttpEntity responseEntity = httpResponse.getEntity();
            byte[] contentBytes = EntityUtils.toByteArray(responseEntity);
            return new String(contentBytes, chatset);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.abort();
            post.releaseConnection();
        }
        return null;
    }

    @Override
    public String post(String url, String charset, File file, String mediaType, Map<String, String> parameters) {
        HttpClientBuilder create = HttpClientBuilder.create();
        HttpClient client = create.build();
        RequestConfig requestConfig = getRequestConfig();
        HttpPost post = new HttpPost(url);
        post.setConfig(requestConfig);
        try {
            FileBody bin = new FileBody(file,ContentType.create(mediaType));
            MultipartEntityBuilder mpEntityBuilder = MultipartEntityBuilder.create(); // 文件传输
            mpEntityBuilder.addPart("file", bin);
            for(String key : parameters.keySet()){
                mpEntityBuilder.addTextBody(key, parameters.get(key));
            }
            HttpEntity httpEntity = mpEntityBuilder.build();
            post.setEntity(httpEntity);
            HttpResponse httpResponse = client.execute(post);
            HttpEntity responseEntity = httpResponse.getEntity();
            byte[] contentBytes = EntityUtils.toByteArray(responseEntity);
            return new String(contentBytes, charset);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.abort();
            post.releaseConnection();
        }
        return null;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值