Java 调用 WebService 服务的 3 种方式
短信预约 -IT技能 免费直播动态提醒
虽然 WebService 这个框架已经过时,但是有些公司还在使用,在调用他们的服务的时候就不得不面对各种问题,本篇文章总结了最近我调用他司 WebService 的心路历程。3 种方式可以分别尝试,哪种能通用哪个。
1. HttpClient
依赖:
org.projectlombok lombok true org.apache.httpcomponents.client5 httpclient5 5.1.3
代码:
import lombok.extern.slf4j.Slf4j;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.HttpEntity;@Slf4jpublic class HttpClientUtil { public static String callServiceHC(String wsUrl, String jsonStr) { String xml = createSoapContent(jsonStr); String returnDatabase = doPostSoap(wsUrl, xml, ""); log.info("returnDatabase===>{}", returnDatabase); return returnDatabase; } public static String createSoapContent(String jsonStr) { log.info("开始拼接请求报文"); //开始拼接请求报文 StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("\n"); stringBuilder.append(" \n"); stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append("getInfo \n"); stringBuilder.append("xml \n"); stringBuilder.append("测试 \n"); stringBuilder.append("AcsaoP21Lxw5KAoQu6SLs624bhGjwNL0DzxsQ9a7B/HbqNsPPcA== \n"); stringBuilder.append(" \n"); stringBuilder.append("]]> \n"); stringBuilder.append(" \n"); stringBuilder.append(" \n"); stringBuilder.append(" \n"); stringBuilder.append(" "); log.info("拼接后的参数"+stringBuilder.toString()); return stringBuilder.toString(); } public static String doPostSoap(String url, String soap, String SOAPAction) { //请求体 String retStr = ""; // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(url); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", SOAPAction); StringEntity data = new StringEntity(soap, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = closeableHttpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印响应内容 retStr = EntityUtils.toString(httpEntity, "UTF-8"); } // 释放资源 closeableHttpClient.close(); } catch (Exception e) { e.printStackTrace(); } return retStr; }}
注意:拼接 xml 字符串的时候要根据实际的 WebService 地址拼接,可在 soapui 中导入 wsurl 获取到入参,如下:
把这些参数全部拼接进去:
2. Http post
依赖:
org.projectlombok lombok true org.jodd jodd-http 6.3.0
代码:
import lombok.extern.slf4j.Slf4j;import jodd.http.*;public class HttpPostUtil { public static String callServiceHP(String wsUrl, String jsonStr) { String xml = createSoapContent(jsonStr); String uploadFeeDetailJsonStr = postWs(wsUrl, xml); return uploadFeeDetailJsonStr; } public static String createSoapContent(String jsonStr) { log.info("开始拼接请求报文"); //开始拼接请求报文 StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("\n"); stringBuilder.append(" \n"); stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append("\n"); stringBuilder.append("getInfo \n"); stringBuilder.append("xml \n"); stringBuilder.append("测试 \n"); stringBuilder.append("AcsaoP21Lxw5KAoQu6SLs624bhGjwNL0DzxsQ9a7B/HbqNsPPcA== \n"); stringBuilder.append(" \n"); stringBuilder.append("]]> \n"); stringBuilder.append(" \n"); stringBuilder.append(" \n"); stringBuilder.append(" \n"); stringBuilder.append(" "); log.info("拼接后的参数"+stringBuilder.toString()); return stringBuilder.toString(); } public static String postWs(String url, String jsonStr) { HttpResponse resp = HttpRequest.post(url).connectionTimeout(60000).timeout(60000) .contentType("application/xml", StandardCharsets.UTF_8.toString()) .header("SOAPAction","") .bodyText(jsonStr, "application/xml", "utf-8") .charset(StandardCharsets.UTF_8.toString()).trustAllCerts(true) .send(); resp.charset(StandardCharsets.UTF_8.toString()); return resp.bodyText(); }}
3. cxf
因为我用 cxf 调不通,所以就在这里就直接奉上其他博主的调用案例:
https://blog.csdn.net/qq_20161461/article/details/116237450
来源地址:https://blog.csdn.net/m0_54355172/article/details/130348073
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341