• 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();
        }
        
    }
  • 相关阅读:
    (转)js的左右滑动触屏事件
    (转)Document对象内容集合
    AppCan相关网站
    (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解
    (转)深入浅出 iOS 之生命周期
    (转)iphone数据存储之-- Core Data的使用
    (转)xcode5.0.2下国际化图文解说
    (转)IOS之Info.plist文件简介
    Note_Master-Detail Application(iOS template)_06_ YJYDetailViewController.h
    Note_Master-Detail Application(iOS template)_07_ YJYDetailViewController.m
  • 原文地址:https://www.cnblogs.com/zfzf1/p/10599106.html
Copyright © 2020-2023  润新知