1. 程式人生 > >Java RPC模式客戶端 WebService

Java RPC模式客戶端 WebService

1.專案環境:WebService服務端Delphi開發,客戶端Eclipse JDK1.8

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>