java protobuffer post

postUtil

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class ProtoUtils {
    protected final static Logger logger = LoggerFactory.getLogger(ProtoUtils.class);

    private static PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager();

    static{
        //设置最大连接数不超过200
        cm.setMaxTotal(200);
        //每个路由默认的连接数20
        cm.setDefaultMaxPerRoute(20);
        //路由最大连接数不超过50

        HttpHost locaHost=new HttpHost("localhost",80, "http://");
        HttpRoute route=new HttpRoute(locaHost);
        cm.setMaxPerRoute(route, 50);
    }

    public static byte[] post(String url, byte[] date){
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "x-protobuf;charset=UTF-8");
        try {
            HttpEntity httpEntity = post(url, null, headers, new ByteArrayEntity(date));
            return EntityUtils.toByteArray(httpEntity);
        } catch (Exception e) {
            logger.error("Http post protobuf failed:"+url, e);
        }
        return date;
    }

    public static HttpEntity post(String url, Map<String, String> paraments, Map<String, String> headers, HttpEntity entity){
        RequestBuilder requestBuilder = RequestBuilder.post().setUri(url);
        if(headers != null){
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                requestBuilder.setHeader(entry.getKey(), entry.getValue());
            }
        }
        if(paraments != null){
            for (Map.Entry<String, String> entry : paraments.entrySet()) {
                requestBuilder.addParameter(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        if(entity != null){
            requestBuilder.setEntity(entity);
        }
        HttpUriRequest  httpUriRequset = requestBuilder.build();
        try {
            return execute(url, httpUriRequset);
        } catch (Exception e) {
            httpUriRequset.abort();
            logger.error("Http post protobuf failed:"+url, e);
        }
        return null;
    }
    
    private static HttpEntity execute(String url, HttpUriRequest httpUriRequset) throws ClientProtocolException, IOException{

        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        CloseableHttpResponse httpResponse = httpClient.execute(httpUriRequset);
        StatusLine httpStatus = httpResponse.getStatusLine();
        if(httpStatus.getStatusCode() != 200){
            if(httpResponse != null){
                EntityUtils.consume(httpResponse.getEntity());
                httpResponse.close();
            }
        }
        return httpResponse.getEntity();
    }
    
}

 

posted @ 2019-03-26 11:36  zfzf1  阅读(494)  评论(0编辑  收藏  举报