okHttp3+cxf 调WebService接口
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.xml.sax.InputSource;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.concurrent.TimeUnit;
@Slf4j
public class WebServiceUtils {
public static Object sendMessage(Object message, String operName, String wsdlUrl) {
try {
Object[] response = sendWebService(wsdlUrl, operName, message);
return response[0];
} catch (Exception e) {
log.error(e.toString(), e);
throw new RuntimeException("调用接口异常");
}
}
/***
* SOAP1.2发送消息
*
* @param wsdlUrl wsdl地址
* @param operName 方法名称
* @param message 内容
* @return
* @throws Exception
*/
private static Object[] sendWebService(String wsdlUrl, String operName, Object message) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient(wsdlUrl);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
//连接超时毫秒
httpClientPolicy.setConnectionTimeout(30000L);
//取消块编码
httpClientPolicy.setAllowChunking(false);
//响应超时
httpClientPolicy.setReceiveTimeout(30000L);
http.setClient(httpClientPolicy);
Endpoint endpoint = client.getEndpoint();
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operName);
BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
if (bindingInfo.getOperation(opName) == null) {
for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
if (operName.equals(operationInfo.getName().getLocalPart())) {
opName = operationInfo.getName();
break;
}
}
}
Object[] response = client.invoke(opName, message);
return response;
}
/**
* SOAP1.1发送消息
*
* @param wsdlUrl wsdl地址
* @param operName 方法名称
* @param opDetail 内容
* @return
* @throws Exception
*/
public static String sendWebService(String wsdlUrl,String operName ,String opDetail) throws Exception {
String errorDesc =null;
String response = null;
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(180, TimeUnit.SECONDS)
.callTimeout(180, TimeUnit.SECONDS)
.readTimeout(180, TimeUnit.SECONDS)
.build();
MediaType mediaType = MediaType.parse("text/xml");
okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, opDetail);
Request request1 = new Request.Builder()
.url(wsdlUrl)
.post(body)
.addHeader("SOAPAction", "")
.addHeader("Content-Type", "text/xml; charset=utf-8")
.addHeader("cache-control", "no-cache")
.build();
Response response1 = client.newCall(request1).execute();
int code = response1.code();
if (200 == code) {
String xmlResult = response1.body().string();
response = xmlResult;
}else{
errorDesc=response1.body().toString();
}
return response;
}
}
添加pom.xml依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.1</version>
</dependency>