(1)使用Axis
import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class AxisWebService { public static void main(String[] args) { try { String endpoint = "http://www.webservicex.net/globalweather.asmx?WSDL"; String nameSpace = "http://www.webserviceX.NET"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName(new QName(nameSpace, "GetWeather")); call.addParameter(new QName(nameSpace,"CityName"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.addParameter(new QName(nameSpace,"CountryName"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.setUseSOAPAction(true); call.setReturnType(org.apache.axis.Constants.XSD_STRING); call.setSOAPActionURI("http://www.webserviceX.NET/GetWeather"); String result = (String) call.invoke(new Object[] {"chengdu","china"}); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
(2)使用XFire
import java.net.MalformedURLException; import java.net.URL; import org.codehaus.xfire.client.Client; public class XFireWebService { public static String invokeService(){ String endpoint = "http://www.webservicex.net/globalweather.asmx?WSDL"; Client client = null; try{ client = new Client(new URL(endpoint)); Object[] result = client.invoke("GetWeather", new Object[] {"chengdu","china"}); return (String)result[0]; }catch(Exception e){ } return ""; } public static void main(String[] args) { System.out.println(XFireWebService.invokeService()); } }
二、js访问WebService
(1)使用ActiveXObject
function requestByGet(){ var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //Webservice location. var URL="http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=chengdu&CountryName=china"; xmlhttp.Open("GET",URL, false); xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.SetRequestHeader ("SOAPAction","http://http://www.webserviceX.NET/GetWeather"); xmlhttp.Send(null); var result = xmlhttp.status; //OK if(result==200) { alert(xmlhttp.responseText); } xmlhttp = null; }
function requestByPost(city,country){ var data; data = '<?xml version="1.0" encoding="utf-8"?>'; data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; data = data + '<soap:Body>'; data = data + '<GetWeather xmlns="http://www.webserviceX.NET">'; data = data + '<CityName>'+city+'</CityName>'; data = data + '<CountryName>'+country+'</CountryName>'; data = data + '</GetWeather>'; data = data + '</soap:Body>'; data = data + '</soap:Envelope>'; var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); var URL="http://www.webservicex.net/globalweather.asmx"; xmlhttp.Open("POST",URL, false); xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.SetRequestHeader ("SOAPAction","http://www.webserviceX.NET/GetWeather"); xmlhttp.Send(data); alert( xmlhttp.responseText); }
(2)使用jQuery的ajax
$(function(){ $.ajax({ type: "GET",//GET or POST url: "http://www.webservicex.net/globalweather.asmx/GetWeather", ////Webservice location data: {CityName: 'chengdu', CountryName: 'china'}, dataType: "xml", success: function(data, textStatus){ var xml = data.xml.replace(/</g,"<").replace(/>/g,">"); alert(xml); //alert($(xml).find("Status").length); alert($(xml).find("Temperature").eq(0).text()); $(xml).find("Status").each(function(i,obj){ //alert(obj.innerText); alert($(this).text()); }); }, error:function(XMLHttpRequest, textStatus, errorThrown){ alert(textStatus); } }); });
三、在oracle中调用WebService
(1)使用UTL_HTTP
CREATE OR REPLACE FUNCTION TEST_GET_WEATHER( cityname IN VARCHAR2, countryname IN VARCHAR2 )RETURN VARCHAR2 AS env VARCHAR2(32767); http_req utl_http.req; http_resp utl_http.resp; return_value xmltype; error_value xmltype; error_code VARCHAR(256); error_string VARCHAR2(32767); result_string varchar2(32767); BEGIN env := '<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetWeather xmlns="http://www.webserviceX.NET"> <CityName>' || cityname || '</CityName> <CountryName>' || countryname || '</CountryName> </GetWeather> </soap:Body> </soap:Envelope> '; http_req := utl_http.begin_request('http://www.webservicex.net/globalweather.asmx?WSDL', 'POST', 'HTTP/1.0'); utl_http.set_header(http_req,'Content-Type','text/xml'); utl_http.set_header(http_req,'Content-Length',length(env)); utl_http.set_header(http_req,'SOAPAction','http://www.webserviceX.NET/GetWeather'); utl_http.write_text(http_req,env); http_resp := utl_http.get_response(http_req); utl_http.read_text(http_resp,env); utl_http.end_response(http_resp); return_value := xmltype.createxml(env).extract('/soap:Envelope/soap:Body/child::node()','xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'); error_value := return_value.extract('/soap:Fault','xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'); if( error_value is not null) THEN error_string := error_value.extract('/soap:Fault/faultstring/child::text()','xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"').getstringval(); error_code := error_value.extract('/soap:Fault/faultcode/child::text()','xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"').getstringval(); raise_application_error(-20000,'error in authentification: ' || error_string || ' - ' || error_code); end if; result_string := return_value.getStringVal(); dbms_output.put_line(result_string); result_string := replace(result_string, '<', '<'); result_string := replace(result_string, '>', '>'); result_string := replace(result_string, '"', '"'); return result_string; END;
(2)使用UTL_DBWS
例子: Consuming Web Services in Oracle 10g
说明:oracle自己的例子成功了,调用GetWeather的WebService,没成功。