前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HttpClient 发送Json

HttpClient 发送Json

作者头像
shengjk1
发布2018-10-24 15:26:30
2.2K0
发布2018-10-24 15:26:30
举报
文章被收录于专栏:码字搬砖码字搬砖
代码语言:javascript
复制
    import com.fasterxml.jackson.databind.ObjectMapper;
    import msxf.model.People;
    import msxf.until.ImpalaJdbc;
    import msxf.until.NowDate;
    import org.apache.commons.configuration.PropertiesConfiguration;
    import org.apache.http.HttpStatus;
    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.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.protocol.HTTP;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

   /**
     * Created by shengjk1  on 2016/6/23.
     */
    public class Main {
        private final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Main.class);
        private static long c =System.currentTimeMillis();

        public static void main(String[] args) {

            try {
                //impala 查询返回,peopleList
                List<People> peopleList= ImpalaJdbc.connImpala();
                Map ma=new HashMap();
                ma.put("sendtime", NowDate.nowDate());
                if(peopleList.size()==0){
                    logger.info("peopleList.size()==0");
                }
                ma.put("data",peopleList);
                ObjectMapper om=new ObjectMapper();
                PropertiesConfiguration pro =new PropertiesConfiguration("conf.properties");
                String serviceAddr=pro.getString("serviceAddr");
                logger.info("serviceAddr "+serviceAddr);

                //将对象变为json
                String jsonStr=om.writeValueAsString(ma);
    //          System.out.println(jsonStr);

                CloseableHttpResponse httpResponse=null;
                CloseableHttpClient httpClient= HttpClientBuilder.create().setRetryHandler(new DefaultHttpRequestRetryHandler()).build();

                //设置超时时间
                HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
                factory.setConnectTimeout(1000*60*6); //单位毫秒
                factory.setReadTimeout(1000*60*5);



                //解决中文乱码
                StringEntity stringEntity=new StringEntity(jsonStr,"UTF-8");
                stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                HttpUriRequest httpUriRequest= RequestBuilder.post(serviceAddr).setEntity(stringEntity).build();

                httpResponse=httpClient.execute(httpUriRequest);
                logger.info(" 发送 ");
                int statusCode=httpResponse.getStatusLine().getStatusCode();
                if(statusCode== HttpStatus.SC_OK){
    //              HttpEntity entity = httpResponse.getEntity();
    //              InputStream in =entity.getContent();
                    logger.info("====文件传输服务器正常响应!");
                    long d =System.currentTimeMillis();
                    logger.info("结束 "+(d-c)/1000 +" s");

                }else {
                    logger.info("====文件传输服务器未正常响应!");

                }

            }catch (Exception e) {
                logger.error(e);

            }
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年10月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档