调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下
axis方式调用:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://www.my.com/Rpc");
//Integer k = (Integer) call.invoke(new Object[] { i, j });
//System.out.println("result is " + k.toString() + ".");
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
soap方式调用
调用java生成的webservice
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
返回一维数组时
Parameter soapResult = soapResponse.getReturnValue();
String[] temp = (String[])soapResult.getValue();
调用ASP.Net生成的webservice
private String HelloWorld(String uri, String u) {
try {
SOAPMappingRegistry smr = new SOAPMappingRegistry();
StringDeserializer sd = new StringDeserializer();
ArraySerializer arraySer = new ArraySerializer();
BeanSerializer beanSer = new BeanSerializer();
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
"http://tempuri.org/", "HelloWorldResult"), String.class,
null, sd);
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
"http://tempuri.org/", "temp"), String.class,
beanSer, beanSer);
URL url = new URL(uri);
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setTargetObjectURI("urn:xmethods-Service1");
call.setMethodName("HelloWorld");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
Vector soapParams = new Vector();
soapParams.addElement(new Parameter("temp", String.class, u, null));
call.setParams(soapParams);
Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
System.out.println(fault);
} else {
Parameter soapResult = soapResponse.getReturnValue();
Object obj = soapResult.getValue();
System.out.println("===" + obj);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
分享到:
相关推荐
超简单的webservice调用demo,对hutool工具进行简单的封装,只需要输入webservice接口名、接口参数即可进行webservice接口调用,欢迎大家吐槽
WebService调用的几种方式,总结了Axis,Cxf,HttpClient,Myeclipse反向生成,XFire调用方式。
webservice调用方法手册简单教程 chm格式
WebService调用代码样例
WebService调用方法类,具体调用代码,使用案例
Webservice调用工具 Soap调用工具
java 调用https发布的webservice接口实例及axis包及详细说明
首先我们创建一个WebService,里面有两个方法:一个无参的方法,一个有参的方法: 创建好了WebService以后,把WebService部署到IIS上,并确保可以访问 二、静态引用 这种方式是通过添加静态引用的方式调用...
.net调用webservice接口例子.net调用webservice接口例子.net调用webservice接口例子.net调用webservice接口例子
soap调用返回的数据经常放在:<webservice方法名Result>XXX</webservice方法名Result>中,在webservice调用中已经直接提取出来了,提取出来的内容还是一个xml,这段内容的处理,单独写在xml解析类里面 (2)对...
调用WebService,最简单的办法当然是直接添加WEB引用,然后自动产生代理类,但是在调用JAVA的WebService时并没有这么简单,特别是对于SoapHeader的处理,通过C#添加Web引用方式访问JavaWebService的方法,除了string...
本代码介绍了vc6.0如何调用webservice的详细步骤,是通过自己的调试成功 总结下来的
使用WSDL生成webService接口,编写Test调用方法进行测试
java调用webservice接口方法,直接AXIS调用远程的web service
WebService调用测试工具,支持大部分wsdl调用。
我们可以使用System.Reflection命名空间中的MethodInfo类来调用WebService方法。 需要引用的名称空间 在动态调用WebService时,我们需要引用以下四个名称空间: * System.Web.Services.Description * System....
支持webservice调用借口数据开发,能够支持任何消息协议,json,xml,字符串等等格式
WebService调用技术文档
lua实现webservice调用 通过gsoap 实现 C/C++ 调用web service
webservice调用实例,通过HttpClient调用,包括完整参数调用,项目通过maven搭建