王朝网络
分享
 
 
 

Webservice调用方式

王朝学院·作者佚名  2009-12-30  
宽屏版  字体: |||超大  

调用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/jimmy292/archive/2009/12/28/5089335.aspx

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有