Java Calls PHP Soap service method

Source: Internet
Author: User
Tags class operator
Java Calls PHP Soap service

WebService is a cross-platform, remote invocation method. One party provides services and the other uses the services. The transport protocol is HTTP, and the transmitted data is data in XML format.

It has two ways, WSDL and soap. Only soap is currently mentioned.

1.PHP SOAP Service available

Defines a class that contains addition, subtraction, and three operations

 
  


Use the PHP soap API to provide SOAP services. (need to turn on soap EXTENSION)

 
  "http://localhost:8089"); $server->setclass ("Operator"); $server->handle ();? >


Assuming PHP for service is index.php

2.JAVA SOAP service provided with PHP

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{//Establish connection soapconnectionfactory = Soapconnectionfactory.newinstance (); soapconnection = Soapconnectionfactory.createconnection (); messagefactory = Messagefactory.newinstance (); soapMessage = Messagefactory.createmessage (); Soappart =Soapmessage.getsoappart (); SoapEnvelope = Soappart.getenvelope ();//Set the WebService method of the call, and the reference 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 ();//Gets the return value 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.getchildn Odes (). Item (0). Getnodename (). Equals ("return")) {List
 
  > returnarray = new ArrayList
  
   > (); for (int i=0;i
   
     
     


operation are as follows:

3+4:
#text: 7
9-7:
#text: 2
20/4:
#text: 5

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.