Java WebService RPC模式客户端实例

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值