Webservice调用方式

Webservice调用方式:axis,soap详解

调用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;
}
/**
  * 调用 C# 的webservice接口
  * SoapRpcMethod(Action = "http://www.tangs.com/Add",
  * RequestNamespace = "http://www.tangs.com/T",
  * ResponseNamespace = "http://www.tangs.com/T",
  * Use = SoapBindingUse.Literal)]
  *
  * */
 public static void addTest() {
  try {
   Integer i = 1;
   Integer j = 2;

   // WebService URL
   String service_url = "http://localhost:4079/ws/Service.asmx";

   Service service = new Service();
   Call call = (Call) service.createCall();
   call.setTargetEndpointAddress(new java.net.URL(service_url));

   // 设置要调用的方法
   call.setOperationName(new QName("http://www.tangs.com/T", "Add"));

   // 该方法需要的参数
   call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);
   call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);

   // 方法的返回值类型
   call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);

   call.setUseSOAPAction(true);
   call.setSOAPActionURI("http://www.tangs.com/Add");

   // 调用该方法
   Integer res = (Integer) call.invoke(new Object[] { i, j });

   System.out.println("Result: " + res.toString());

  } catch (Exception e) {
   System.err.println(e);
  }
 }

 /**
  * 调用 C# 的webservice接口
  * SoapRpcMethod(Action = "http://www.tangs.com/Add",
  * RequestNamespace = "http://www.tangs.com/T",
  * ResponseNamespace = "http://www.tangs.com/T",
  * Use = SoapBindingUse.Literal)]
  *
  * */
 public static void helloTest() {
  try {

   String endpoint = "http://localhost:4079/ws/Service.asmx";
   Service service = new Service();
   Call call = (Call) service.createCall();
   call.setTargetEndpointAddress(new java.net.URL(endpoint));
   call.setOperationName(new QName("http://www.tangs.com/T", "HelloWorld"));

   call.setUseSOAPAction(true);
   call.setSOAPActionURI("http://www.tangs.com/Hello");

   String res = (String) call.invoke(new Object[] { null });

   System.out.println("Result: " + res);
  } catch (Exception e) {
   System.err.println(e.toString());
  }
 }


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx

WebService是一种面向Web服务的通信协议,可以使不同的应用程序通过Web进行通信。在Java中,可以使用JAX-WS API和Apache CXF框架来调用WebService。下面是一个简单的WebService调用示例: ```java import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL; public class WebServiceClient { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/MyWebService?wsdl"); QName qname = new QName("http://example.com/", "MyWebServiceImplService"); Service service = Service.create(url, qname); MyWebService myWebService = service.getPort(MyWebService.class); String result = myWebService.sayHello("World"); System.out.println(result); } } ``` 在上面的代码中,我们首先创建了一个URL对象,用于引用WebService的WSDL文件。然后我们创建了一个QName对象,用于指定WebService的服务名称和命名空间。接下来,我们使用Service.create()方法创建了一个Service对象,该对象用于与WebService进行通信。最后,我们使用Service.getPort()方法获取与WebService通信的实例,并调用其方法。 需要注意的是,MyWebService是一个接口,它定义了与WebService通信的方法。在调用WebService时,我们需要使用服务端实现该接口的类的实例来调用其方法。在上面的代码中,我们使用了服务端的MyWebServiceImpl类来实现MyWebService接口。 以上就是一个简单的WebService调用示例。需要注意的是,具体的WebService调用方式和实现可能因WebService的类型和实现方式而异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值