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

本文介绍了如何使用Java 8和soap.jar库来调用WebService。通过示例代码展示了调用远程和本地服务的过程,包括设置参数、命名空间和处理响应。在调用过程中遇到了返回值为空的问题,可能需要解决参数命名空间的设置问题。
摘要由CSDN通过智能技术生成

packagecom.inspur.ws;importjava.net.URL;importjava.util.Vector;importorg.apache.soap.Constants;importorg.apache.soap.Fault;importorg.apache.soap.encoding.SOAPMappingRegistry;importorg.apache.soap.encoding.soapenc.StringDeserializer;importorg.apache.soap.rpc.Call;importorg.apache.soap.rpc.Parameter;importorg.apache.soap.rpc.Response;importorg.apache.soap.util.xml.QName;/*** soap调用WebService

**/

public classSoap {private static String url = "http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl";private static String targetNamespace = "http://webxml.com.cn/";/*** 调用在线的服务,返回值为空,参数没传过去,估计需要设置参数的命名空间,但又没找到相关方法

*@paramparam*/

public static voidcall(String param) {try{

Call call= newCall();

SOAPMappingRegistry registry= newSOAPMappingRegistry();

registry.mapTypes(Constants.NS_URI_SOAP_ENC,new QName(targetNamespace, "toTraditionalChineseResult"), null, null, newStringDeserializer());

call.setSOAPMappingRegistry(registry);

call.setTargetObjectURI(targetNamespace);

call.setMethodName("toTraditionalChinese");

call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

Vector params = new Vector();//没找到参数设置命名空间的方法

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();

}

}/*** 调用本地服务成功

*@paramparam*/

public static voidcall2(String param) {try{

Call call= newCall();

SOAPMappingRegistry registry= newSOAPMappingRegistry();

registry.mapTypes(Constants.NS_URI_SOAP_ENC,new QName("", "return"), null, null, newStringDeserializer());

call.setSOAPMappingRegistry(registry);

call.setTargetObjectURI("http://ws.zsyw.inspur.com/");

call.setMethodName("hello");

call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

Vector params = new Vector();

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 voidmain(String[] args) {

call("小学");//返回空

call2("大学");//hello,大学

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值