httpClient 包冲突遇到的的一些怪异问题

最近项目用到http请求系统,来获取值,我用的是

CloseableHttpClient client = HttpClients.createDefault();
        HttpPost request = new HttpPost(url);
       try {
            HttpEntity entity = new StringEntity(jsonStr, "UTF-8");
            request.setHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(entity);
            CloseableHttpResponse response = null;
            
                response = client.execute(request);
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            } catch (Exception e) {
            log.error("发送json格式的post请求失败", e);
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                log.error("关闭http client失败", e);
            }
        },用到CloseableHttpClient这个对象,因为他是4.3.6以后新增的方法。由于项目中以前有4.2.3包.

遇到问题是:

刚开始调用main方法时,方法调用没问题。Myelcipse 启动调用就报错INSTANCE null,最后观察发现是调用4.2.3包。可能myeclipse部署后加载包有先后顺序,走的是4.2.3.解决办法是把4.2.3包删除就OK了。

posted @ 2016-07-04 18:44  IT一族  阅读(517)  评论(0)    收藏  举报