1.项目环境:WebService服务端Delphi开发,客户端Eclipse JDK1.8, 需要Jar包activation.jar,mail.jar,soap.jar
2.WebService接口服务信息,输入地址http://ServerIP:8081/,访问内容如下:
HGXSWebServer - PortTypes:
IWeChat [WSDL]
~MOP_PatOutHosList--测试方法名
~MOP_PatOperList--测试方法名
~NetTest--测试方法名
3.点进2当中的WSDL超链接内容如下:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" name="IWeChatservice" targetNamespace="http://tempuri.org/">
<message name="NetTest41Request">
<part name="AParam" type="xs:string"/>
</message>
<message name="NetTest41Response">
<part name="return" type="xs:string"/>
</message>
<operation name="NetTest">
<soap:operation soapAction="urn:WeChatIntf-IWeChat#NetTest" style="rpc"/>
<input message="tns:NetTest41Request">
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:WeChatIntf-IWeChat"/>
</input>
<output message="tns:NetTest41Response">
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:WeChatIntf-IWeChat"/>
</output>
</operation>
</binding>
<service name="IWeChatservice">
<port name="IWeChatPort" binding="tns:IWeChatbinding">
<soap:address location="http://192.168.19.223:8081/soap/IWeChat"/>
</port>
</service>
</definitions>
注意:name="AParam" type="xs:string",name="return" type="xs:string"和代码有对应关系
4.客户端代码
//请求服务
public Map<String, Object> request(String url, String xmldata,String methodname) {
try {
URL urladr;
urladr = new URL(url);
Call soapCall = new Call();
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
soapCall.setMethodName(methodname);
Vector soapParams = new Vector();
System.out.println("xmldata ="+xmldata);
Parameter paramcontent = new Parameter("AParam", String.class, xmldata, null);//对应了AParam,否则服务端收不到入参
soapParams.addElement(paramcontent);
soapCall.setParams(soapParams);
Response soapResponse = soapCall.invoke(urladr, "");
System.out.println("soapResponse= "+soapResponse.generatedFault());
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String faultcode = fault.getFaultCode();
String faultstring = fault.getFaultString();
System.out.println("Request Fail faultcode= " + faultcode);
System.out.println("Request Fail faultstring= " + faultstring);
} else {
Parameter soapResult = soapResponse.getReturnValue();
System.out.println("Request Success= " + soapResult.getValue().toString());
}
} catch (Exception e) {
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HisServiceRequest testobj = new HisServiceRequest();
String url = "http://192.168.19.223:8081/soap/IWeChat";
String xmldata = "<Request><UserId>888</UserId><Sign></Sign></Request>";
testobj.request(url,xmldata,"NetTest");
}
5.测试返回日志
xmldata =<Request><UserId>888</UserId><Sign></Sign></Request>
soapResponse= false
Request Success=
<?xml version="1.0" encoding="utf-8"?>
<response>
<returnresult>
<returncode>1</returncode>
<errormsg>||</errormsg>
</returnresult>
<data>
<data_row>
<CURRDATETIME>2018-10-24 09:09:02</CURRDATETIME>
</data_row>
</data>
</response>