webservice
来源:互联网 发布:黑客网络 生涯起点 编辑:程序博客网 时间:2024/06/13 21:45
调用某公司的.net服务总是失败报错,最好只能用axis2生成客户端代码,然后加上红色的代码就可以了。
ReceiveStorageStub stub = new ReceiveStorageStub();
stub._getServiceClient().engageModule(org.apache.axis2.Constants.MODULE_ADDRESSING);
ReceiveRealEstateStorageQueue queue = ReceiveRealEstateStorageQueue.class.newInstance();
queue.setStrBizMsgID(strBizMsgID);
queue.setStrRealEstateXML(sendXml);
queue.setStrRecType(strRecType);
queue.setStrReportMethod(srm);
returnValue = stub.receiveRealEstateStorageQueue(queue).getReceiveRealEstateStorageQueueResult();
工具类:
import java.net.URL;
import java.text.MessageFormat;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import org.apache.axis.encoding.XMLType;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
/**
*
* @author zy
*
*/
public class WebServiceUtil {
/**
* seee#org.apache.axis.client.Call
*/
private static Call call = null;
/** Axis2 RPCServiceClient */
private static RPCServiceClient serviceClient;
/**
* 格式 消息的信息.
* @see java.text.MessageFormat#format(String, Object...)
*
* @param pattern 需要格式的字符串
* @param arguments 格式式参数
* @return 成功返回格式化后的内容, 否则不进行格式化操作
*/
public static String messageFormat(String pattern, Object ... arguments) {
try {
return MessageFormat.format(pattern, arguments);
} catch (Exception e) {
return pattern;
}
}
/**
* 使用Axis Call Client动态调用WebService地址.
*
* @param webServiceAddr WebService地址
* @param webServiceMethod WebService方法
* @param soapActionURI SOAPActionURI
* @param targetNamespace 命名空间
* @param inputValues 输入参数值
* @param inputNames 输入参数名称
* @param inputXmlTypes 输入参数XML类型
* @param inputJavaTypes 输入参数JAVA类型
* @return 成功返回<code>Object</code>, 失败或异常返回null.
*/
public static Object getObjectByAxisCallClient(String webServiceAddr, String webServiceMethod, String soapActionURI,
String targetNamespace, Object[] inputValues, String[] inputNames, String[] inputXmlTypes, Class<?>[] inputJavaTypes) {
return getObjectByAxisCallClient(webServiceAddr, webServiceMethod, soapActionURI, targetNamespace, inputValues, inputNames,
inputXmlTypes, inputJavaTypes, null, null);
}
/**
* 使用Axis Call Client动态调用WebService地址.
*
* @param webServiceAddr WebService地址
* @param webServiceMethod WebService方法
* @param soapActionURI SOAPActionURI
* @param targetNamespace 命名空间
* @param inputValues 输入参数值
* @param inputNames 输入参数名称
* @param inputXmlTypes 输入参数XML类型
* @param inputJavaTypes 输入参数JAVA类型
* @param username 用户名(HTTP Basic Authentication)
* @param password 密码(HTTP Basic Authentication)
* @return 成功返回<code>Object</code>, 失败或异常返回null.
*/
public static Object getObjectByAxisCallClient(String webServiceAddr, String webServiceMethod, String soapActionURI,
String targetNamespace, Object[] inputValues, String[] inputNames, String[] inputXmlTypes, Class<?>[] inputJavaTypes,
String username, String password) {
Object resObj = null;
try {
if (call == null) {
Service service = new Service();
call = (Call) service.createCall();
}
// 设置wsdl
call.setTargetEndpointAddress(new URL(webServiceAddr));
// 定义参数对象
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI + webServiceMethod);
// 设置访问的方法名
call.setOperationName(webServiceMethod);
OperationDesc oper = new OperationDesc();
if (!ArrayUtils.isEmpty(inputNames)) {
for (int i = 0; i < inputNames.length; i++) {
// String类型
oper.addParameter(new QName(targetNamespace, inputNames[i]), new QName(targetNamespace, inputXmlTypes[i]),
inputJavaTypes[i], ParameterDesc.IN, false, false);
}
}
oper.setReturnType(XMLType.XSD_ANYTYPE);
call.setOperation(oper);
call.setTimeout(30000);
// 访问权限验证.
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
call.setUsername(username);
call.setPassword(password);
}
resObj = call.invoke(inputValues);
} catch (Exception e) {
// TODO Auto/generated catch block
String message = messageFormat("使用AxisCallClient调用WebService地址{{0}}方法{{1}}出现错误!", webServiceAddr, webServiceMethod);
//throw new InterfaceException(message);
}
return resObj;
}
/**
* 通过Axis2 取得Web Services 结果.
* 可以使用身份验证(HTTP Basic Auchntication)
*
* @param webServiceAddr Web Service的URL地址(需要加?wsdl)
* @param webServiceMethod WebService所操作的方法名称
* @param targetNamespace WebServiceNamespace
* @param inputValues 对所操作方法所需要输入的参数值,如果些操作方法无参则 使用 new Object[] {},不允许输入 null
* @param returnJavaTypes 指定所返回结果数据的Java Class类型数组, 如果无参数则使用 new Object[] {},不允许输入 null
* @return 成功返回<code>Object[]</code>, 失败或异常返回null.
*/
public static Object[] geObjectByAxis2RPCClient(String webServiceAddr, String webServiceMethod, String targetNamespace,
Object[] inputValues, Class<?>[] returnJavaTypes) {
return geObjectByAxis2RPCClient(webServiceAddr, webServiceMethod, targetNamespace, inputValues, returnJavaTypes, null, null);
}
/**
* 通过Axis2 取得Web Services 结果.
* 可以使用身份验证(HTTP Basic Auchntication)
*
* @param webServiceAddr Web Service的URL地址(需要加?wsdl)
* @param webServiceMethod WebService所操作的方法名称
* @param targetNamespace WebServiceNamespace
* @param inputValues 对所操作方法所需要输入的参数值,如果些操作方法无参则 使用 new Object[] {},不允许输入 null
* @param returnJavaTypes 指定所返回结果数据的Java Class类型数组, 如果无参数则使用 new Object[] {},不允许输入 null
* @param username 用户名(HTTP Basic Auchntication)
* @param password 密码(HTTP Basic Auchntication)
* @return 成功返回<code>Object[]</code>, 失败或异常返回null.
*/
public static Object[] geObjectByAxis2RPCClient(String webServiceAddr, String webServiceMethod, String targetNamespace,
Object[] inputValues, Class<?>[] returnJavaTypes, String username, String password) {
Object[] resultObjArr = null;
try {
// 使用RPC方式调用WebService
if (serviceClient == null) {
serviceClient = new RPCServiceClient();
}
Options options = serviceClient.getOptions();
options.setAction(targetNamespace + webServiceMethod);
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(webServiceAddr);
options.setTo(targetEPR);
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
//身份验证(HTTP Basic Auchntication)
HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator();
basicAuth.setPreemptiveAuthentication(true);
basicAuth.setUsername(username);
basicAuth.setPassword(password);
options.setProperty(HTTPConstants.AUTHENTICATE, basicAuth);
}
//连接超时间设置 1min.
options.setTimeOutInMilliSeconds(60000);
QName opAddEntry = new QName(targetNamespace, webServiceMethod);
resultObjArr = serviceClient.invokeBlocking(opAddEntry, inputValues, returnJavaTypes);
} catch (Exception e) {
// TODO: handle exception
resultObjArr = null;
String message = messageFormat("使用Axis2RPCClient调用WebService地址{{0}}方法{{1}}出现错误!", webServiceAddr, webServiceMethod);
//throw new InterfaceException(message);
}
return resultObjArr;
}
/**
*
* 用于能调用方法,参数传不过去的情况
* @param webServiceAddr Web Service的URL地址(需要加?wsdl)
* @param webServiceMethod WebService所操作的方法名称
* @param targetNamespace WebServiceNamespace
* @param inputValues 所操作方法的参数值
* @return
*/
public static Map<String, Object> getByAxis2ServiceClient(String webServiceAddr,
String webServiceMethod, String targetNamespace, Object[] params,
Object[] inputValues) {
try {
ServiceClient sender = new ServiceClient();
EndpointReference endpointReference = new EndpointReference(webServiceAddr);
Options options = new Options();
options.setAction(targetNamespace + webServiceMethod);
options.setTo(endpointReference);
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
// 这个和qname差不多,设置命名空间
OMNamespace omNs = fac.createOMNamespace(targetNamespace, webServiceMethod);
// 参数值
OMElement data = fac.createOMElement(webServiceMethod, omNs);
for (int i = 0; i < params.length; i++) {
OMElement inner = fac.createOMElement((String) params[i], omNs);
inner.setText((String) inputValues[i]);
data.addChild(inner);
}
// 发送数据,返回结果
OMElement result = sender.sendReceive(data);
return getResults(result);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* axis2 OMElement转map
* @param element axis2 OMElement对象
* @return
*/
public static Map<String, Object> getResults(OMElement element) {
if (element == null) {
return null;
}
Iterator iter = element.getChildElements();
Map<String, Object> map = new HashMap<String, Object>();
while (iter.hasNext()) {
OMNode omNode = (OMNode) iter.next();
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
String key = omElement.getLocalName().trim();
String value = omElement.getText().trim();
map.put(key, value);
}
}
return map;
}
}
0 0
- WebService
- WebService
- webservice
- webservice
- WebService
- webservice
- webService
- WebService
- WebService
- webService
- WEBService
- webService
- WebService
- [WebService]
- webservice
- WebService
- webservice
- WebService
- Spring注解方式注入解析
- C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
- unity判断其它物体相对于自身方位以及角度
- blender源码编译——(win10,VS2015环境)
- Spring Boot 常用注解
- webservice
- centos下使用yum出现database disk image is malformed错误
- LibEvent中文帮助文档--第11章【高级话题】
- 使用sonar进行代码质量管理
- 现代需求-进度
- Android中activity与Fragment通信
- MySQL控制(关闭、打开)自动提交功能
- 掰开揉碎机器学习系列-决策树(2)-CART决策树
- Highlight Glow System v5插件的使用