java调用外部接口

java调用webService接口

通过创建下面的方法,调用方法即可,返回值为接收到的xml字符串。

// point  webService接口地址  ;  params  入参 ,通过soupUI生成的参数,soapAction  可以传空字符""
 public static String doPostWebServiceURL(String point, String params, String soapAction)throws Exception {
        String result = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(point);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(params,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            log.error("调用远程WebService接口异常:{}" , e);
            throw e;
        }finally {
            IOUtils.closeQuietly(closeableHttpClient);
        }
        return result;
    }

java通过POST方法调用http请求

public static String httpPost(String url, String data) {
    	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    	RequestConfig requestConfig =  RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();

        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
    	if (StringUtils.isNotBlank(data)){
            StringEntity entity = new StringEntity(data, Charset.forName("UTF-8"));
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
        }
        
        logger.info("HttpUtil.httpPost: url={} "+url+", data={} "+data);
        try {
            logger.info("HttpUtil.httpPost:" + httpPost.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
            	HttpEntity responseEntity= httpResponse.getEntity();
                String response = EntityUtils.toString(responseEntity, "utf-8");
                EntityUtils.consume(responseEntity);
                logger.info("请求[" + url + "]返回:" + response);
                return response;
            } else {
                logger.error("fail ---- " + statusCode);
            }
        } catch (Exception e) {
            logger.error("HttpUtil.httPost error :" + e.getMessage(), e);
        } finally {
        	httpPost.releaseConnection();
        }
        return null;
    }

调用:

Map<String,Object> parameters = new HashMap<String,Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime",endTime);
        String result = HttpUtils.httpPost(url, JsonMapper.toJsonString(parameters));

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值