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

}

}

c 访问java webservice_java调用webservice .相关推荐

  1. java 读取webservice_java 调用webService的各种方法

    一.利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务 1.首先建立一个Web services EndPoint: package Hello; import ...

  2. java axis2 开发webservice_利用Axis2开发WebService(3)—用Java实现调用WebService的客户端程序 | 学步园...

    WebService是为程序服务的,只在浏览器中访问WebService是没有意义的.因此,在本节使用Java实现了一个控制台程序来调用上一节发布的WebService.调用WebService的客户 ...

  3. Java远程调用WebService接口

    WebService简介 Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间, ...

  4. java axis2 调用webservice 接口_Axis2 调用Webservice 接口 | 学步园

    调用方法: TranslatorString  输入中文,翻译成 拼音.英文. 参数:wordKey(中文) 现在要做,翻译词:[随便],代码如下: package cn.com.webxml; im ...

  5. js java webservice_js调用webservice中的方法实现思路及代码

    webservice代码: using System; using System.Web; using System.Collections; using System.Web.Services; u ...

  6. java调用net的webservice_java和.net互相调用webservice注意事项

    最后在研究GWT,需要用到.net 的webservice,网上搜了点代码,基本上问题多多. Java要调用.net的webservice是需要一个axis的包的. 需要下载,然后引用. 引用的时候, ...

  7. java soap协议头_自己调用webservice方法总结(带请求头SoapHeader)

    调用webservice总结:1.加入第三方的jar包 Ksoap2-android-XXX2.访问响应的webservice的网站,查看响应的信息,得到nameSpace,methodName,ur ...

  8. axis2 webservice入门学识(JS,Java,PHP调用实例源码)

    来源:http://www.myexception.cn/web/952419.html axis2 webservice入门知识(JS,Java,PHP调用实例源码) 背景简介 最近接触到一个银行接 ...

  9. java用axis方式调用webservice接口

    最近需要使用webservice接口,所以总结了一下全过程,希望能够对大家有所帮助. 开发使用的项目框架是ssm框架,tomcat服务器. 首先我们会拿到一个调用webservice接口的网址,类似这 ...

最新文章

  1. Windows Server AppFabric Caching
  2. python教程书籍-有什么Python学习的书籍和学习资源推荐?
  3. php 命名空间 create_function,PHP create_function()注入命令执行漏洞
  4. SpringBoot整合Mybatis完整详细版
  5. 大数据-MapReduce计算框架
  6. easyexcel 导入指定_阿里巴巴EasyExcel使用(3)-导入
  7. position_css
  8. java微信公众号支付示例
  9. 全国信息技术水平计算机程序设计c,2010年(上)全国信息技术水平考试计算机程序设计技术水平证书(C语言)考试试卷...
  10. 计算机应用基础模块3实操题正确答案,国开20秋计算机应用基础作业3 模块4 PowerPoint 2010实操题答案...
  11. 坐标转换并导出KML文件
  12. php ssl证书安装,PHPWAMP如何开启SSL,Apache下如何安装ssl证书?配置ssl证书很简单...
  13. QCA-WIFI技术研讨
  14. cad断点快捷键_CAD打断和打断于点怎么使用?CAD打断快捷键命令及操作方法
  15. git创建分支develop,并合并到master
  16. icloud验证失败连接服务器时出现问题,登录 iCloud 提示验证失败连接到服务器时出现问题怎么办及苹果iPhone手机安装两个微信教程...
  17. 新能源充电桩主板二代新上市,迎来充电桩产业新一轮发展
  18. 有没有什么赚钱的副业?分享,适合学生赚钱的30个副业!
  19. S3C2440 I2C总线控制
  20. Ubuntu16.04 + Cuda-9.0 + Cudnn-7.1.4 + TensorFlow1.8(极其简单)

热门文章

  1. 【深度学习在智能机器人中的应用】论文合集推荐丨CMU新型机器人算法可操纵所有日常家具
  2. Adobe系列错误代码解决方案汇总,遇到这些问题该怎么办
  3. 如何用AXURE制作简单的幻灯片播放
  4. golang生成随机ID的方法之ObjectID
  5. oracle程序包或函数处于无效状态,ORA-06575: 程序包或函数 NO_VM_DROP_PROC 处于无效状态...
  6. IE首页被篡改怎么恢复
  7. 基于JAVA视听小说计算机毕业设计源码+数据库+lw文档+系统+部署
  8. 利用TensorFlow搭建CNN
  9. HTC G8 wildfire第一次刷机
  10. P2P网贷戏台倒了,转型中的互金中概股如何把戏唱下去?