WebService系列之HttpClient调用WebService接口
测试工具下载soapui测试
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
package com.extra.credit.util;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import java.nio.charset.Charset;
import java.util.List;/*** <pre>* 工具类:HttpClient方式调用webService服务* </pre>** <pre>* @author nicky ma* 修改记录* 修改后版本: 修改人: 修改日期: 2021/03/11 10:05 修改内容:* </pre>*/
@Component
public class WSHttpClientUils {static Logger log = LoggerFactory.getLogger(WSHttpClientUils.class);/*** HttpClient方式调用webservice api <br>* @Author mazq* @Date 2021/03/11 10:14* @Param [point, params, methodName]* @return java.lang.String*/public static String doPostWebServiceURL(String point, String params, String soapAction)throws Exception {String result = "";// 创建HttpClientBuilderHttpClientBuilder httpClientBuilder = HttpClientBuilder.create();// HttpClientCloseableHttpClient 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;}/*** xml转换为JSONObject <br>* @Author nicky ma* @Date 2021/03/11 10:01* @Param [xmlStr]* @return com.alibaba.fastjson.JSONObject*/public static JSONObject xml2Json(String xmlStr) throws DocumentException {Document doc = DocumentHelper.parseText(xmlStr);JSONObject json = new JSONObject();doParseXmlElements(doc.getRootElement() , json);return json;}/*** xml Document Elements解析 <br>* @Author nicky ma* @Date 2021/03/11 10:01* @Param [element, json]* @return void*/public static void doParseXmlElements(Element element, JSONObject json) {List<Element> chdEl = element.elements();for(Element e : chdEl){if (!e.elements().isEmpty()) {JSONObject chdjson = new JSONObject();doParseXmlElements(e, chdjson);Object o = json.get(e.getName());if (o != null) {JSONArray jsona = null;if (o instanceof JSONObject) {JSONObject jsono = (JSONObject) o;json.remove(e.getName());jsona = new JSONArray();jsona.add(jsono);jsona.add(chdjson);}if (o instanceof JSONArray) {jsona = (JSONArray) o;jsona.add(chdjson);}json.put(e.getName(), jsona);} else {if (!chdjson.isEmpty()) {json.put(e.getName(), chdjson);}}} else {if (!e.getText().isEmpty()) {json.put(e.getName(), e.getText());}}}}}
protected Map<String , String> obtainTokenByHttp(String paramJson , String url) throws Exception {StringBuffer soapRequestParams = new StringBuffer();soapRequestParams.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.webservice.catalog.infotrust.com\">");soapRequestParams.append("<soapenv:Header/>");soapRequestParams.append("<soapenv:Body>");soapRequestParams.append("<ser:getToken>");soapRequestParams.append("<ser:in0>"+paramJson+"</ser:in0>");soapRequestParams.append("</ser:getToken>");soapRequestParams.append("</soapenv:Body>");soapRequestParams.append("</soapenv:Envelope>");logger.warn(String.format("request params:%s", soapRequestParams.toString()));String returnDatabase = WSHttpClientUils.doPostWebServiceURL(url , soapRequestParams.toString(),"");logger.warn(String.format("xml string:%s" , returnDatabase));com.alibaba.fastjson.JSONObject jsonObject = WSHttpClientUils.xml2Json(returnDatabase);com.alibaba.fastjson.JSONObject body = jsonObject.getJSONObject("Body");com.alibaba.fastjson.JSONObject getSafeTokenResponse = body.getJSONObject("getSafeTokenResponse");String out = getSafeTokenResponse.getString("out");com.alibaba.fastjson.JSONObject outJson = JSON.parseObject(out);String code = outJson.getString("code");String message = outJson.getString("message");String token = "";logger.warn(String.format("webservice api返回数据,code:%s,message:%s",code,message));if ("200".equals(code)) {token = outJson.getString("token");}if (logger.isInfoEnabled()) {logger.info(String.format("api token:%s", token));}Map<String,String> result = new HashMap<String, String>(2);result.put("token",token);return result;}
WebService系列之HttpClient调用WebService接口相关推荐
- HttpClient调用WebService接口
文章目录 前言 WebService是什么? WebService原理? HttpClient是什么? HttpClient应用场景? 使用HttpClient调用WebService接口 1.创建H ...
- JAVA使用HttpClient调用webservice接口
JAVA使用HttpClient调用webservice接口 关于HttpClient用法参考: HttpClient-4.5.2官方教程完整翻译 官方教程原文链接 HttpClient下载地址: 链 ...
- android调用web接口,Android调用WebService系列之请求调用
好久没写博客,快有3年了.当初想在CTO把自己的技术文章一直延续,可却没有坚持! 开发Android网络App,通讯方式决定了你App所需的能力! 最近正在用Web Service进行通讯,那么就让我 ...
- .Net 4.0使用httpClient调用WebApi接口时提示“由于远程方已关闭传输流,身份验证失败。”
.Net 4.0使用httpClient调用WebApi接口时提示"由于远程方已关闭传输流,身份验证失败." 参考https://blog.csdn.net/qiaohuyue/a ...
- android调用webservice传参数,android调用webservice接口获取信息
我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...
- ionic云开发系列二之调用webservice
Part 1:怎么访问后台呢 在上一篇文章 ionic云开发系列一 已经介绍了在云上搭建ionic框架了,如果你还不知道怎么搭建ionic本地开发环境, 请移步ionic Getting Starte ...
- HTTP 调用 WebService、CXF 动态调用 WebService
概述 Web Service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用 ...
- java使用httpclient调用上传图片接口[示例]
参考网站:nodejs使用http模块编写上传图片接口测试客户端 如果是java控制台app可以前往http://hc.apache.org/downloads.cgi下载新版httpclient库 ...
- js php调用webservice,js跨域调用WebService的使用方法
这次给大家带来js跨域调用WebService的使用方法,js跨域调用WebService的注意事项有哪些,下面就是实战案例,一起来看一下. 步骤1. 在web.config中的system.web节 ...
- HttpClient调用http接口(POST)
调用方法,传过去接口地址和参数(json对象或者map,在方法里转成json字符串) JSONObject.toJSONString(map) JSONObj.toString() public st ...
最新文章
- 第一届中国三维视觉大会China3DV论文展示
- BugKuCTF WEB 矛盾
- 类方法classmethod 绑定方法
- Linux学习进阶!!
- php 输出text格式化,php printf() 输出格式化的字符串,phpprintf
- java linux 服务_java项目部署Linux服务器几种启动方式总结经验
- Traveling on the Axis 模拟
- [转]HTTP协议之状态码详解
- 这届618:商家全渠道作战,天猫仍是主场
- 比特大陆招股书曝光;阿里否认阻挠滴滴收购OFO;Uber巨资了结数据泄露案| 雷锋早报...
- 致远oa系统unix 服务器,致远oa如何设置服务器地址
- kettle 同步Oracle 与 Postgres
- creo 二次开发 protookit 官方make file 案例试运行
- 安卓自定义 View 进阶: 图片文字
- AutoJs学习-包名查看器
- Antd组件库的使用
- Processing Arduino 音频频谱显示
- 微服务架构之全局异常(@ControllerAdvice + @ExceptionHandler)
- 高德地图-添加一个或多个覆盖物
- 进军餐饮行业,店铺该如何选址?