• 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();
        }
        
    }
  • 相关阅读:
    接口表与临时表的用途
    mac电脑连接oracle报错ora-24454,客户主机名未设置
    项目管理口径与法人管理口径会计分录公司信息生成问题
    关于接口的一些理解
    梳理EBS系统中上下文的概念和用法
    数据库系统的用途浅析
    EBS与外围系统数据的交互方式——接口表与API的区别
    四年EBS系统顾问风雨之路回顾——002话
    Web服务器处理请求过程浅谈
    ZOOKEEPER+KAFKA 集群搭建
  • 原文地址:https://www.cnblogs.com/zfzf1/p/10599106.html
Copyright © 2020-2023  润新知