ocgn

导航

RPC方式调用远程webservice接口

/**
* 可调整调用方法与命名空间的请求
* @param wsMethod 方法名
* @param bodyMessage json请求体.toString()
* @return JSONObject
*/
public static JSONObject sendMessage( String wsMethod, String bodyMessage) {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient;
JSONObject reqBody = new JSONObject();

try {
serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();
// 设置超时时间
options.setTimeOutInMilliSeconds(timeOut);
options.setProperty(HTTPConstants.SO_TIMEOUT, timeOut);
// 指定调用WebService的URL
EndpointReference targetAddr = new EndpointReference(wsUrl);
options.setTo(targetAddr);
options.setAction(wsMethod);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(nameSpace, wsMethod);
OMElement data = fac.createOMElement(wsMethod, omNs);

// 构造参数
OMElement inner = null;
//参数位置在XML中的data标签下
inner = fac.createOMElement("data", omNs);
inner.setText(bodyMessage);
data.addChild(inner);

//返回方法名
wsMethod = wsMethod+"Return";
//返回参数
String reMessage = null;
// 发起调用
logger.info("调用远程EAS接口,请求方法 : "+ wsMethod);
logger.info("调用远程EAS接口,请求参数 : "+ bodyMessage);
OMElement result = serviceClient.sendReceive(data);
Iterator iterator = result.getChildElements();
while (iterator.hasNext()) {
OMElement resultMessage = (OMElement) iterator.next();
if (resultMessage.getLocalName().equals(wsMethod)) {
//获取返回数据
reMessage = resultMessage.getText();
}
}
if (!reMessage.isEmpty()) {
reqBody = JSONObject.parseObject(reMessage);
if (!"ok".equals(reqBody.getString("status"))){
logger.info("调用远程EAS接口失败,返回错误信息 : "+ reqBody.get("status").toString());
return reqBody;
}
logger.info("调用远程EAS接口成功,返回信息 : "+ reqBody.toString());
}

} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}


return reqBody;
}

posted on 2021-02-05 16:56  ocgn  阅读(605)  评论(0编辑  收藏  举报