package com.jdl.lomir.chint.adapter.http.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.commons.codec.binary.base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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 javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@Slf4j
public class WebServiceUtils {
private static int socketTimeout = 30000;// 请求超时时间
private static int connectTimeout = 30000;// 传输超时时间
public static String[] invoke(String address, String method, String targetNamespace, Object[] keys, Object[] values) throws Exception {
Service service = new Service();
Call call = (Call) service.createCall();
call.setEncodingStyle("UTF-8");
call.setTargetEndpointAddress(address);
call.setOperationName(new QName(targetNamespace, method)); // 方法名称
call.setReturnType(XMLType.SOAP_ARRAY);// 设置返回类型
call.setReturnClass(String[].class);
params(targetNamespace, call, keys, values);
call.setUseSOAPAction(true);
call.setSOAPActionURI(targetNamespace + method);
return (String[]) call.invoke(values);
}
private static void params(String targetNamespace, Call call, Object[] keys, Object[] values) {
if (keys == null || values == null) return;
if (keys.length == 0 || values.length == 0) return;
if (keys.length != values.length) {
throw new IllegalArgumentException("接口方法参数与参数值不匹配!");
}
for (int i = 0; i < values.length; i++) {
String key = (String) keys[i]; //方法参数
Object value = values[i]; //参数值
if (value == null) {
call.addParameter(new QName(targetNamespace, key), XMLType.XSD_STRING, ParameterMode.IN);
} else if (value instanceof String) {
call.addParameter(new QName(targetNamespace, key), XMLType.XSD_STRING, ParameterMode.IN);
} else if (value instanceof Integer) {
call.addParameter(new QName(targetNamespace, key), XMLType.XSD_INTEGER, ParameterMode.IN);
} else if (value instanceof Boolean) {
call.addParameter(new QName(targetNamespace, key), XMLType.XSD_BOOLEAN, ParameterMode.IN);
}
}
}
public static String execute(String userName, String passWord, String postUrl, String soapXml, String soapAction) {
String retStr = "";
try {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
httpPost.setHeader("Connection","Keep-Alive");
if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(passWord)) {
String auth = userName + ":" + passWord;
byte[] encodedAuth = base64.encodebase64(
auth.getBytes(StandardCharsets.UTF_8));
String basic = "Basic " + new String(encodedAuth);
httpPost.setHeader("Authorization", basic);
}
StringEntity stringEntity = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
log.error("exception in SOAP1.1 failed to send message!");
}
return retStr;
}
}