http工具类

前言

一个http工具类

代码

import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.util.Map;

public class HttpUtils {

	public interface ResponseCallback {
		void onResponse(CloseableHttpResponse response) throws Exception;
		void onError(int resultCode, String resultStr) throws Exception;
	}

    public static void headerPost(String urlStr, String data, ResponseCallback callback, Map<String, String> headerMap) throws Exception {
		doPost(urlStr, data, callback, headerMap, 300000);
    }

    private static void doPost(String urlStr, String data, ResponseCallback callback, Map<String, String> headerMap, int timeOut) throws Exception {
        HttpUriRequest request = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
			request = new HttpPost(urlStr);
			if (headerMap != null) {
				for(Map.Entry<String, String> entry : headerMap.entrySet()){
					request.setHeader(entry.getKey(), entry.getValue());
				}
			}
			if (null != data && !"".equals(data)) {
				((HttpPost) request).setEntity(new ByteArrayEntity(data.getBytes("utf-8")));
			}
            RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(timeOut).setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
            client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
            response = client.execute(request);
            int resultCode = response.getStatusLine().getStatusCode();
            if (resultCode == HttpStatus.SC_OK) {
            	callback.onResponse(response);
            } else {
                throw new Exception(resultCode + ":system error");
            }
        } catch (Exception e) {
        	callback.onError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString());
        } finally {
            if (request != null && !request.isAborted()) {
                request.abort();
                request = null;
            }
            if (client != null) {
                HttpClientUtils.closeQuietly(client);
                client = null;
            }
            if (response != null) {
                HttpClientUtils.closeQuietly(response);
                response = null;
            }
        }
    }
}
public class PostGo {
    public static String postGo(String urlStr, String appid, String data) throws Exception {
        final Map<String, String> resultMap = new HashMap<String, String>();
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("Content-Type", "application/json");
        if (appid != null) {
            headerMap.put("appid", appid);
        }
        HttpUtils.ResponseCallback callback = new HttpUtils.ResponseCallback() {
            @Override
            public void onResponse(CloseableHttpResponse response) throws Exception {
                try {
                    String resultStr = EntityUtils.toString(response.getEntity(), "utf-8");
                    resultMap.put("resultStr", resultStr);
                } catch (Exception e) {
                    throw new Exception("onResponse error");
                }
            }
            @Override
            public void onError(int resultCode, String resultStr) throws Exception {
                throw new Exception("onError error" + resultCode);
            }
        };
        HttpUtils.headerPost(urlStr, data, callback, headerMap);
        return resultMap.get("resultStr");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值