JAVA调用PHP SOAP服务
来源:互联网 发布:现货行情软件 编辑:程序博客网 时间:2024/06/01 08:18
WebService即是一种跨平台的远程调用方法。一方提供服务,另一方使用服务。传输协议是HTTP,而传输的数据则是XML格式的数据。
它有两种方式,WSDL和SOAP。目前仅提及SOAP。
1.PHP提供SOAP服务
定义一个类,它里面有加、减和除三种操作
<?php class Operator{ public function add($x,$y){ return $x+$y; } public function substract($x,$y){ return $x-$y; } public function divide($x,$y){ return $x/$y; }}?>
使用PHP SOAP API,提供SOAP服务。(需要开启SOAP EXTENSION)
<?php require("Operator.php");$server = new SoapServer(null,array('uri' => "http://localhost:8089"));$server->setClass("Operator");$server->handle();?>
假设提供服务的php为index.php
2.JAVA使用PHP提供的SOAP服务
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import javax.xml.soap.MessageFactory;import javax.xml.soap.Node;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPConnection;import javax.xml.soap.SOAPConnectionFactory;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPEnvelope;import javax.xml.soap.SOAPException;import javax.xml.soap.SOAPMessage;import javax.xml.soap.SOAPPart;public class Main {public void testSOAP(String opename,int x,int y){String url = "http://localhost:8089/index.php";SOAPConnectionFactory soapConnectionFactory = null;SOAPConnection soapConnection = null;MessageFactory messageFactory = null;SOAPMessage soapMessage = null;SOAPPart soapPart = null;SOAPEnvelope soapEnvelope = null;SOAPBody body = null;try{//建立连接soapConnectionFactory = SOAPConnectionFactory.newInstance();soapConnection = soapConnectionFactory.createConnection();messageFactory = MessageFactory.newInstance();soapMessage = messageFactory.createMessage();soapPart = soapMessage.getSOAPPart();soapEnvelope = soapPart.getEnvelope();//设置调用的webservice方法,及传参body = soapEnvelope.getBody();SOAPElement element = body.addChildElement(soapEnvelope.createName(opename));element.addChildElement("in0").addTextNode(String.valueOf(x));element.addChildElement("in1").addTextNode(String.valueOf(y));soapMessage.saveChanges();//获取返回值SOAPMessage reply = soapConnection.call(soapMessage,url);soapPart = reply.getSOAPPart();soapEnvelope = soapPart.getEnvelope();body = soapEnvelope.getBody();Node returnvalue = (Node) body.getChildElements().next();if (returnvalue != null) { if (returnvalue.getChildNodes().item(0).getNodeName().equals("return")) { List<HashMap<String,String>> ReturnArray = new ArrayList<HashMap<String,String>>(); for (int i=0;i<returnvalue.getChildNodes().item(0).getChildNodes().getLength();i++) { String key = returnvalue.getChildNodes().item(0).getChildNodes().item(i).getNodeName(); String value = returnvalue.getChildNodes().item(0).getChildNodes().item(i).getNodeValue(); System.out.println(key+":"+value); } } } else { System.out.println("nothing returned"); }}catch(Exception ex){ex.printStackTrace();}finally{try {soapConnection.close();} catch (SOAPException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {Main main = new Main();System.out.println("3+4:");main.testSOAP("add",3,4);System.out.println("9-7:");main.testSOAP("substract",9,7);System.out.println("20/4:");main.testSOAP("divide",20,4);}}
运行结果如下:
3+4:
#text:7
9-7:
#text:2
20/4:
#text:5
0 1
- JAVA调用PHP SOAP服务
- JAVA调用PHP SOAP服务的示例
- PHP提供Web Service服务供JAVA调用(SOAP+WSDL)
- PHP soap 调用 JAVA发布的webservice
- 客户端调用SOAP服务
- 在 PHP 中使用 SOAP 协议调用 Web服务(WebService)
- PHP : SOAP 协议调用 Web服务(WebService)(转)
- PHP调用SOAP
- php进行soap调用
- 用Perl调用SOAP服务
- PHP SOAP服务示例代码
- C# D2010调用PHP SOAP
- PHP调用webservice soap soapclient
- 使用 AJAX 调用 SOAP Web 服务
- 使用 AJAX 调用 SOAP Web 服务
- 使用 SOAP 协议调用 Web服务(WebService)
- Ajax通过SOAP协议调用Web服务
- VC6.0使用SOAP调用公共服务
- hdu 逃离迷宫
- spark1.1.1 发布
- DMP文件的生成和使用
- Mac上使用命令行安装brew,并通过brew安装Ant等工具
- 用pattern进行自然语言处理
- JAVA调用PHP SOAP服务
- CAD杀毒V2.6 正式版
- 第六十一讲:Android之AIDL(一)
- ubuntu 14.04 64位安装32glib库
- 正则表达式30分钟入门
- Android智能电视应用程序开发浅谈(三)
- Python 视频学习 26-29 正则表达式
- 策略模式
- Cocos2d-x里Javascript开发的一些要点