WebService系列之HttpClient调用WebService接口
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 = "";
// 创建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;
}
/**
* 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());
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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;
}
————————————————
版权声明:本文为CSDN博主「smileNicky」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014427391/article/details/114698297