调用 web service 时生成soap消息的区别,此区别就会造成调用web service失败。
1、使用Axis 1.4调用wsdl中的接口、参数以及传入消息头,以下方法调用时不会把web service 的目标函数名称包含进SOAP消息里头。
import java.net.URL;
import org.apache.axis.client.Stub;
import org.apache.axis.message.SOAPHeaderElement;
import org.xx.www.schema.parlayx.data.sync.v1_0.local.AuthUserBehaviorRequest;
import org.xx.www.schema.parlayx.data.sync.v1_0.local.AuthUserBehaviorResponse;
import org.xx.www.schema.parlayx.data.v1_0.NamedParameter;
import org.xx.www.schema.parlayx.data.v1_0.NamedParameterList;
import org.xx.www.wsdl.parlayx.data.sync.v1_0._interface.UsageAuth;
import org.xx.www.wsdl.parlayx.data.sync.v1_0.service.UsageAuthServiceLocator;
public class ClientTest_UsageAuth_Other {
public static void main(String[] args) {
UsageAuthServiceLocator locator = new UsageAuthServiceLocator();
try {
String url = "http://xxx:8110/UsageAuthService/services/UsageAuth";
AuthUserBehaviorRequest req = new AuthUserBehaviorRequest();
req.setMSISDN("1");
req.setServiceId("2");
req.setFid(null);
NamedParameter np = new NamedParameter();
np.setKey("contentSP");
np.setValue("3");
NamedParameter np1 = new NamedParameter();
np1.setKey("productID");
np1.setValue("1");
NamedParameter np2 = new NamedParameter();
np2.setKey("parentID");
np2.setValue("2");
NamedParameter[] extensionInfo = new NamedParameter[3];
extensionInfo[0] = np;
extensionInfo[1] = np1;
extensionInfo[2] = np2;
req.setExtensionInfo(new NamedParameterList(extensionInfo));
UsageAuth inter = locator.getUsageAuth(new URL(url));
SOAPHeaderElement headerElement = new SOAPHeaderElement("http://xx/schema/common/v2_1", "RequestSOAPHeader");
headerElement.addChildElement("spId").addTextNode("a");
headerElement.addChildElement("spPassword").addTextNode("a");
headerElement.addChildElement("serviceId").addTextNode("a");
headerElement.addChildElement("timeStamp").addTextNode("a");
((Stub) inter).setHeader(headerElement);
try {
AuthUserBehaviorResponse rsp = inter.authUserBehavior(req);
System.out.println(rsp.getResult() + "--------------------------");
System.out.println(rsp.getResultDescription() + "--------------------------");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
产生的SOAP消息为:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://www.xx.com.cn/schema/common/v2_1" xmlns:loc="http://www.xx.org/schema/xx/xx/sync/v1_0/a"> <soapenv:Header> <RequestSOAPHeader xmlns="http://www.xx.com.cn/schema/common/v2_1"> <spId>s</spId> <spPassword>a</spPassword> <serviceId>a</serviceId> <timeStamp>a</timeStamp> </RequestSOAPHeader> </soapenv:Header> <soapenv:Body> <loc:AuthUserBehaviorRequest> <loc:MSISDN>s</loc:MSISDN> <loc:serviceId>s</loc:serviceId> <loc:Fid></loc:Fid> <loc:extensionInfo> <namedParameters> <key>contentSP</key> <value>ss</value> </namedParameters> <namedParameters> <key>productID</key> <value>sa</value> </namedParameters> <namedParameters> <key>parentID</key> <value>sa</value> </namedParameters> </loc:extensionInfo> </loc:AuthUserBehaviorRequest> </soapenv:Body> </soapenv:Envelope>
2、把调用的web service的目标函数名称包含在SOAP消息中。
import java.util.Calendar;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.soap.SOAPElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
import org.xx.www.schema.parlayx.data.sync.v1_0.local.AuthUserBehaviorRequest;
import org.xx.www.schema.parlayx.data.sync.v1_0.local.AuthUserBehaviorResponse;
import org.xx.www.schema.parlayx.data.v1_0.NamedParameter;
import org.xx.www.schema.parlayx.data.v1_0.NamedParameterList;
import com.wichina.common.DateUtils;
public class ClientTest_UsageAuth {
/**
* @param args
*/
public static void main(String[] args) {
try {
String endpointURL = "http://xx:8110/UsageAuthService/services/UsageAuth";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpointURL));
call.setOperationName(new QName("http://aa/schema/parlayx/data/sync/v1_0/local", "AuthUserBehavior"));// 设置操作的名称。
// 添加消息头
SOAPHeaderElement requestSOAPHeader = new SOAPHeaderElement("http://ax/schema/common/v2_1", "RequestSOAPHeader");
SOAPElement spId = requestSOAPHeader.addChildElement("spId");
spId.addTextNode("1");
SOAPElement serviceId = requestSOAPHeader.addChildElement("serviceId");
serviceId.addTextNode("1");
SOAPElement timeStamp = requestSOAPHeader.addChildElement("timeStamp");
timeStamp.addTextNode(DateUtils.getDate(Calendar.getInstance().getTime(), "yyyyMMddHHmmss").toString());
SOAPElement spPassword = requestSOAPHeader.addChildElement("spPassword");
spPassword.addTextNode("2");
// SOAPElement fa = requestSOAPHeader.addChildElement("FA", "tns");
// fa.addTextNode("13799265585");
QName qnParam = new QName("http://xx/schema/parlayx/data/sync/v1_0/local", "p_qn");
call.addHeader(requestSOAPHeader);
call.setReturnType(qnParam);// 返回的数据类型
// 设置请求参数
NamedParameter[] extensionInfoReq = new NamedParameter[3];
NamedParameter namedParameter0 = new NamedParameter();
namedParameter0.setKey("contentSP");
namedParameter0.setValue("a");
extensionInfoReq[0] = namedParameter0;
NamedParameter namedParameter1 = new NamedParameter();
namedParameter1.setKey("productID");
namedParameter1.setValue("x");
extensionInfoReq[1] = namedParameter1;
NamedParameter namedParameter2 = new NamedParameter();
namedParameter2.setKey("parentID");
namedParameter2.setValue("x");
extensionInfoReq[2] = namedParameter2;
AuthUserBehaviorRequest request = new AuthUserBehaviorRequest();
request.setMSISDN("a");
request.setServiceId("x");
request.setExtensionInfo(new NamedParameterList(extensionInfoReq));
call.addParameter("AuthUserBehaviorRequest", qnParam, ParameterMode.IN);// 参数的类型
AuthUserBehaviorResponse rsp = (AuthUserBehaviorResponse) call.invoke(new Object[] { request });// 执行调用
System.out.println(rsp.getResult());
System.out.println(rsp.getResultDescription());
NamedParameterList extensionInfoList = rsp.getExtensionInfo();
NamedParameter[] extensionInfos = extensionInfoList.getNamedParameters();
if (extensionInfos != null) {
for (int i = 0; i < extensionInfos.length; i++) {
System.out.println(extensionInfos[i].getKey() + "=>" + extensionInfos[i].getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成的消息:
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <ns1:RequestSOAPHeader soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="http://xx/schema/common/v2_1"> <ns1:spId>x</ns1:spId> <ns1:serviceId>x</ns1:serviceId> <ns1:timeStamp>x</ns1:timeStamp> <ns1:spPassword>x</ns1:spPassword> </ns1:RequestSOAPHeader> </soapenv:Header> <soapenv:Body> <ns2:AuthUserBehavior soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://xx/schema/parlayx/data/sync/v1_0/local"> <AuthUserBehaviorRequest href="#id0" /> </ns2:AuthUserBehavior> <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:AuthUserBehaviorRequest" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns3="http://xx/schema/parlayx/data/sync/v1_0/local"> <MSISDN xsi:type="xsd:string">x</MSISDN> <serviceId xsi:type="xsd:string">x</serviceId> <Fid xsi:type="xsd:string" xsi:nil="true" /> <extensionInfo href="#id1" /> </multiRef> <multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns4:NamedParameterList" xmlns:ns4="http://xx/schema/parlayx/data/v1_0" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <namedParameters href="#id2" /> <namedParameters href="#id3" /> <namedParameters href="#id4" /> </multiRef> <multiRef id="id3" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns5:NamedParameter" xmlns:ns5="http://xx/schema/parlayx/data/v1_0" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <key xsi:type="xsd:string">productID</key> <value xsi:type="xsd:string">x</value> </multiRef> <multiRef id="id4" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns6:NamedParameter" xmlns:ns6="http://xx/schema/parlayx/data/v1_0" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <key xsi:type="xsd:string">parentID</key> <value xsi:type="xsd:string">x</value> </multiRef> <multiRef id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns7:NamedParameter" xmlns:ns7="http://xx/schema/parlayx/data/v1_0" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <key xsi:type="xsd:string">contentSP</key> <value xsi:type="xsd:string">x</value> </multiRef> </soapenv:Body> </soapenv:Envelope>
第1种方法与第2种方法明显的区别在于,调用时有没有包含调用的web service函数名称。