`
zpball
  • 浏览: 910225 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Webservice调用方式:axis,soap详解

阅读更多
转自:[url] http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]
调用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());
  }
 }
分享到:
评论

相关推荐

    Java Webservice调用方式详解

    Java WebService调用方式详解主要涉及两种方法:Axis和SOAP。这两种方式都是用来与Web服务进行交互,调用远程服务的方法。以下将详细介绍这两种方法。 1. Axis方式调用: Axis是Apache的一个开源项目,它提供了一...

    Java+Webservice调用方式详解

    ### Java+Webservice调用方式详解 #### 一、引言 随着互联网技术的发展与企业级应用需求的增长,Web服务(Webservice)作为一种基于XML的标准协议,在不同平台间实现服务互操作方面扮演着越来越重要的角色。Java...

    详解axis调用webservice实例

    标题中的“详解axis调用webservice实例”表明我们将探讨如何使用Apache Axis库来调用Web服务。Apache Axis是一个开源工具,它允许Java开发者创建、部署和使用Web服务。在这个实例中,我们会有机会看到实际的Java代码...

    Java WebService 简单实例 方式二(axis1直接调用方式)

    ### Java WebService 简单实例 方式二(axis1直接调用方式) #### 背景介绍 在软件开发领域,特别是在企业级应用中,Web服务作为一种标准的技术规范被广泛采用,它允许不同系统间进行通信与数据交换。Java Web ...

    Axis2 WebService常用功能详解

    【Axis2 WebService常用功能详解】 Axis2是一个广泛使用的Web服务引擎,它为开发者提供了创建和部署Web服务的强大工具。由于其灵活性和丰富的特性,Axis2在不同场景下都有着广泛的应用,比如作为服务端接口发布Java...

    WebService详细解析(axis,xfire,cxf,授权认证加密解密)

    【WebService详解】 WebService是一种基于XML的Web应用程序接口标准,它允许不同的系统和服务之间进行互操作,通过HTTP协议传输数据,使得分布式系统间的通信变得更加简单。WebService的核心技术包括SOAP(Simple ...

    WebService------AXIS

    WebService——AXIS详解 在IT领域,WebService是一种基于标准的、平台无关的、可以在不同系统之间交换数据的方式。它利用XML(可扩展标记语言)作为数据格式,HTTP作为传输协议,SOAP(简单对象访问协议)作为消息...

    基于axis2的webservice

    【基于Axis2的Web服务详解】 在信息技术领域,Web服务是一种通过互联网进行通信的软件系统。它们使用标准的协议和格式,如XML(eXtensible Markup Language)和SOAP(Simple Object Access Protocol),使得不同...

    Eclipse用Axis框架开发WebService

    【知识点详解】 1. **Apache Axis**: Axis 是一个开源项目,属于 Apache 软件基金会,它是 SOAP(简单对象访问协议)引擎,专门用于构建 ...通过这种方式,开发者可以快速构建符合 SOAP 标准的、可互操作的 Web 服务。

    客户端编程方式调用webservice

    本篇文章将详细介绍如何通过客户端编程方式使用Java调用WebService。 首先,让我们理解Java中调用WebService的基本步骤: 1. **获取WSDL文件**:WSDL是WebService的接口定义,它描述了服务的位置、服务提供的操作...

    WebService之Axis2教程

    - **实现方式**:通过HTTP SOAP协议调用WebService。 - **项目二:使用Struts2.1.6的Web版PIM**: - **目标**:实现与项目一相同的功能,但采用Web版界面。 - **特点**:展示了如何将WebService集成到Web应用程序...

    axis2搭建webService并包含android调用此WebService服务案例

    ### Android调用eclipse+axis2搭建的webService服务详解 #### Eclipse+axis2+Tomcat搭建webService服务 **准备工作** 在开始之前,确保你有以下工具: 1. **Eclipse**: 用于开发Java项目和webService。可以从...

    axis+webservice学习教程

    ### 使用AXIS开发WebService教程详解 #### 一、前言 本文将详细介绍如何利用Apache Axis框架来开发WebService。Apache Axis是Apache软件基金会的一个开源项目,它提供了强大的工具集用于实现和部署基于SOAP协议的...

    java axis webservice 开发实例

    Java Axis WebService 开发实例详解 在Java世界中,开发Web服务时,Axis是一个非常流行的开源工具,它允许开发者创建、部署和使用Web服务。本实例将深入探讨如何使用Axis来构建一个简单的Web服务,并进行调用。这个...

    webservice for axis客户端详尽代码

    本资源“webservice for axis客户端详尽代码”提供了一套完整的Axis客户端代码示例,对于学习和理解如何在Java应用中使用Axis来调用Web服务极具价值。 一、Web Service基础知识 Web服务通常通过SOAP(Simple ...

Global site tag (gtag.js) - Google Analytics