Java调用WebService方法总结(8)--soap.jar调用WebService

时间:2024-01-18 21:21:02

Apache的soap.jar是一种历史很久远的WebService技术,大概是2001年左右的技术,所需soap.jar可以在http://archive.apache.org/dist/ws/soap/version-2.3.1/下载;本文简单记录下其调用WebService的方法。文中所使用到的软件版本:Java 1.8.0_191、soap 2.3.1。

1、准备

参考Java调用WebService方法总结(1)--准备工作

2、调用

这种方式调用本地服务成功了,但调用在线的服务时返回值总是为空,参数没传过去,估计需要设置参数的命名空间,但又没找到相关方法。

package com.inspur.ws;

import java.net.URL;
import java.util.Vector; import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.encoding.soapenc.StringDeserializer;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import org.apache.soap.util.xml.QName; /**
* soap调用WebService
*
*/
public class Soap {
private static String url = "http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl";
private static String targetNamespace = "http://webxml.com.cn/"; /**
* 调用在线的服务,返回值为空,参数没传过去,估计需要设置参数的命名空间,但又没找到相关方法
* @param param
*/
public static void call(String param) {
try {
Call call = new Call();
SOAPMappingRegistry registry = new SOAPMappingRegistry();
registry.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(targetNamespace, "toTraditionalChineseResult"), null, null, new StringDeserializer());
call.setSOAPMappingRegistry(registry); call.setTargetObjectURI(targetNamespace);
call.setMethodName("toTraditionalChinese");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); Vector<Parameter> params = new Vector<Parameter>();
//没找到参数设置命名空间的方法
params.addElement(new Parameter("sText", String.class, param, null));
call.setParams(params); Response response = call.invoke(new URL(url), "http://webxml.com.cn/toTraditionalChinese");
if (response.generatedFault()) {
Fault fault = response.getFault();
System.out.println(fault.getFaultCode() + "|" + fault.getFaultString());
} else {
System.out.println(response.getReturnValue() + "|" + response.getReturnValue().getValue());//第二个参数为SOAPAction
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 调用本地服务成功
* @param param
*/
public static void call2(String param) {
try {
Call call = new Call();
SOAPMappingRegistry registry = new SOAPMappingRegistry();
registry.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("", "return"), null, null, new StringDeserializer());
call.setSOAPMappingRegistry(registry); call.setTargetObjectURI("http://ws.zsyw.inspur.com/");
call.setMethodName("hello");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); Vector<Parameter> params = new Vector<Parameter>();
params.addElement(new Parameter("name", String.class, param, null));
call.setParams(params); Response response = call.invoke(new URL("http://10.40.103.48:9006/zsywservice/TestService?wsdl"), "");
if (response.generatedFault()) {
Fault fault = response.getFault();
System.out.println(fault.getFaultCode() + "|" + fault.getFaultString());
} else {
System.out.println(response.getReturnValue());
}
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
call("小学");//返回空
call2("大学");//hello,大学
}
}