java 调用soapui_Java动态调用Webservice,不生成客户端,基于soapUI

转自 https://blog.csdn.net/u014087208/article/details/79927574?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

package com.sonic.platform.soap;

/***

*

*

*

*

*

*/

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.codec.binary.Base64;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.lang3.StringUtils;

import org.apache.http.HttpEntity;

import org.apache.http.ParseException;

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 org.apache.xmlbeans.XmlException;

import com.eviware.soapui.impl.wsdl.WsdlInterface;

import com.eviware.soapui.impl.wsdl.WsdlOperation;

import com.eviware.soapui.impl.wsdl.WsdlProject;

import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;

import com.eviware.soapui.model.iface.Operation;

import com.eviware.soapui.support.SoapUIException;

import com.google.common.base.Preconditions;

import com.google.common.collect.Lists;

import net.sf.json.xml.XMLSerializer;

/**

*

* @author FishingGo

*

*/

public class ConvertWSDL{

private final static String password_key="soapui.loader.password";

private final static String username_key="soapui.loader.username";

/**

*

* @return

* @throws XmlException

* @throws IOException

* @throws SoapUIException

*/

private static WsdlProject init() throws XmlException, IOException, SoapUIException{

//new 会有一个守护进程???

WsdlProject WSDL_PROJECT=new WsdlProject();

return WSDL_PROJECT;

}

/**

* auth

* @param userName

* @param password

* @param auth

*/

private static void initAuth(String userName,String password,boolean auth){

if(auth){

Preconditions.checkArgument(StringUtils.isNotBlank(userName), "username can not be empty or null");

Preconditions.checkArgument(StringUtils.isNotBlank(password), "password can not be empty or null");

System.setProperty(password_key, password);

System.setProperty(username_key, userName);

}

}

/**

* 解析WSDL

* @param address

* @param method

* @param userName

* @param password

* @param auth

* @return

* @throws ConvertWSDLException

*/

private final static OperationMethodInfo operationMethodInfo(String address,String method,String userName,String password,boolean auth) throws ConvertWSDLException{

Preconditions.checkArgument(StringUtils.isNotBlank(method), "method can not be empty or null");

List infos=convertOperation(address, userName, password, auth);

for (OperationMethodInfo operationMethodInfo : infos) {

if(method.equals(operationMethodInfo.getOperationName())) return operationMethodInfo;

}

return null;

}

public final static OperationMethodInfo operationMethodInfo(String address,String method) throws ConvertWSDLException{

return operationMethodInfo(address, method,null,null,false);

}

public final static OperationMethodInfo operationMethodInfo(String address,String method,String userName,String password) throws ConvertWSDLException{

return operationMethodInfo(address, method,userName,password,true);

}

/**

* 解析WSDL 返回SOAP协议参数XML

* @param address

* @param method

* @return

* @throws ConvertWSDLException

*/

public final static String soapRequestXML(String address,String method) throws ConvertWSDLException{

OperationMethodInfo operationMethodInfo=operationMethodInfo(address, method);

return null==operationMethodInfo?null:operationMethodInfo.getRequestXml();

}

public final static String soapRequestXML(String address,String method,String userName,String password) throws ConvertWSDLException{

OperationMethodInfo operationMethodInfo=operationMethodInfo(address, method,userName,password);

return null==operationMethodInfo?null:operationMethodInfo.getRequestXml();

}

/**

* convert WSDL Operation

* @param address

* @param charset

* @param userName

* @param password

* @param auth

* @return

* @throws ConvertWSDLException

*/

private static List convertOperation(String address,String userName,String password,boolean auth) throws ConvertWSDLException{

initAuth(userName, password, auth);

WsdlInterface wsdlInterface=getWsdlInterface(address);

return convertOperation(wsdlInterface);

}

public final static List convertOperation(String address) throws ConvertWSDLException{

return convertOperation(address,null,null,false);

}

public final static List convertOperation(String address,String userName,String password) throws ConvertWSDLException{

return convertOperation(address,userName, password, true);

}

/**

* convert WSDL Operation

* @param wsdlInterface

* @return

*/

private static List convertOperation(WsdlInterface wsdlInterface) {

List operationList = wsdlInterface.getOperationList();

return new ArrayList<>(Lists.transform(operationList, action->{

return new OperationMethodInfo((WsdlOperation) action);

}));

}

/**

* getWsdlInterface

* @param address

* @return

* @throws ConvertWSDLException

*/

private static WsdlInterface getWsdlInterface(String address) throws ConvertWSDLException {

Preconditions.checkArgument(StringUtils.isNotBlank(address), "address can not be empty or null");

try {

WsdlInterface[] wsdls = WsdlImporter.importWsdl(init(), address.endsWith("wsdl")?address:address+"?wsdl");

return wsdls[0];

} catch (XmlException e) {

throw new ConvertWSDLException(e);

} catch (IOException e) {

throw new ConvertWSDLException(e);

} catch (SoapUIException e) {

throw new ConvertWSDLException(e);

} catch (Exception e) {

throw new ConvertWSDLException(e);

}

}

final static String charset="UTF-8";

private static final String xmlToJson(String responseXml){

if (responseXml != null && !"".equals(responseXml)) {

int beginIndex = responseXml.indexOf("");

int endIndex = responseXml.indexOf("");

responseXml = responseXml.substring(beginIndex, endIndex+9);

}

return StringUtils.isNotBlank(responseXml)?new XMLSerializer().read(responseXml).toString():null;

}

/**

*

* @param address

* @param charset

* @param requestXml

* @throws MalformedURLException

* @throws IOException

*/

@SuppressWarnings("unused")

@Deprecated

private static String doPostSoap_(String address,String charset,String requestXml) throws MalformedURLException, IOException{

HttpURLConnection httpURLConnection=null;

try {

httpURLConnection=(HttpURLConnection) new URL(address).openConnection();

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setDoOutput(true);

httpURLConnection.setDoInput(true);

httpURLConnection.setRequestProperty("Content-Type","text/xml;charset="+charset);

OutputStream outputStream=httpURLConnection.getOutputStream();

outputStream.write(requestXml.getBytes(charset));

int responseCode = httpURLConnection.getResponseCode();

StringBuffer stringBuffer=new StringBuffer();

if(HttpStatus.SC_OK==responseCode){

InputStream inputStream=httpURLConnection.getInputStream();

byte[] buffer = new byte[1024];

int len = 0;

while ((len = inputStream.read(buffer)) > 0) {

stringBuffer.append(new String(buffer, 0, len));

}

}

return xmlToJson(stringBuffer.toString());

} finally {

if(null!=httpURLConnection) httpURLConnection.disconnect();

}

}

public static String doPostSoap(String address,String requestXml) throws ParseException, IOException{

return doPostSoap(address,requestXml,charset);

}

public static String doPostSoap(String address,String requestXml,String charset) throws ParseException, IOException{

return doPostSoap(address,charset, requestXml,null,null,null,false);

}

public static String doPostSoap(String address,String requestXml,String charset,String username,String password) throws ParseException, IOException{

return doPostSoap(address,charset, requestXml,null,username,password,true);

}

public static String doPostSoap(String address,String requestXml,String username,String password) throws ParseException, IOException{

return doPostSoap(address,requestXml,charset,username,password);

}

/**

*

* @param url

* @param charset

* @param requestXml

* @param methodName

* @param username

* @param password

* @param auth

* @return

* @throws ParseException

* @throws IOException

*/

private static String doPostSoap(String url, String charset, String requestXml, String methodName, String username,

String password, boolean auth) throws ParseException, IOException {

CloseableHttpClient httpClient = null;

try {

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

httpClient = httpClientBuilder.build();

HttpPost httpPost = new HttpPost(url);

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60 * 1000)

.setConnectTimeout(60 * 1000).build();

httpPost.setConfig(requestConfig);

httpPost.setHeader("Content-Type", "text/xml;charset=" + charset);

//httpPost.setHeader("SOAPAction", methodName);

if (auth)

httpPost.setHeader("Authorization", authHeader(username, password, charset));

StringEntity data = new StringEntity(requestXml, Charset.forName(charset));

httpPost.setEntity(data);

CloseableHttpResponse response = httpClient.execute(httpPost);

if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {

HttpEntity httpEntity = response.getEntity();

if (httpEntity != null) {

return xmlToJson(EntityUtils.toString(httpEntity, charset));

}

}

} finally {

// 释放资源

httpClient.close();

}

return null;

}

private static String authHeader(String userName, String password, String charset) {

String auth = userName + ":" + password;

byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName(charset)));

String authHeader = "Basic " + new String(encodedAuth);

return authHeader;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值