一、Java访问WebService
(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,没成功。
分享到:
相关推荐
Webservice创建,访问代码实例,多种方式
Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice. 1. webservice远程...
Apache CXF是一个开源的Service框架,它实现了JCP与Web Service中...CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。
支持基于HTTP、JMS、XMPP等多种协议访问Web服务; 支持多种Web服务业界重要标准如SOAP、WSDL、Web服务寻址(WS-Addressing)、Web服务安全(WS-Security)等; 支持JSR181,可以通过JDK5配置Web服务; 高性能的SOAP...
本系统以Visual Studio 2008为开发工具,使用SuperMap的多种组件实现了地图开发,并通过服务器端的WebService服务在手机端实现了对服务器端的远程实时访问。介于系统的综合服务性质,对于物流业有广泛的应用前景。
正如您下面将要了解到这些技术提供了多种选择您可以将这些选择结合起来以获得额外效果例如可以将防火墙 和 XML Web Service 起使用从而根据客户端身份以及为他们所建立相应规则来限制对某些功能(思路方法)访问
前几章我们分别利用spring rmi、httpinvoker、httpclient、webservice技术实现不同服务器间的远程访问。本章我将通过spring jms和activemq实现单Web项目服务器间异步访问和多Web项目服务器间异步访问。 一. 简介 1. ...
WebService-WS WS服务独立发布 搭建一个独立的 maven(jar) 服务项目 导入CXF jar 包支持 引入 log4j.properties 文件 @WebService使用类上面,标记类是webservice服务提供对象 @WebMethod使用在方法上面,...
随着SOA理念的流行和 WebService等技术的广泛应用,我们发现在越来越多的系统中,我们需要访问各种不同的底层数据,这些数据包括关系型数据库,EJB组件,XML文件或数据库,Web服务, JSP 页面数据等等。为了能够访问...
asp.net ajax调用aspx、ajax调用ashx处理类方法、ajax调用webservice方法,返回多种数据类型等xml、html等。
CWSS以其高扩展的架构体系闻名,包括内置SOTP通讯协议、访问用户管理和后台多种群集功能。 3)快速开发 CWSS独特的设计体系模式,可简化开发,加速投放市场速度。并可利用已有技能,迅速部署应用系统。 4)部署更趋...
SiteMapIMS-DOTNET 的整体设计上采用微软DotNet的WebService 和XML 技术,数据存取利用ADO.NET支持Access、SQL-Server、Oracle等主流数据库。另外由于C#对Exception(异常)处理、内存管理优化等采用的多项先进技术,...
业务组要求ASF提供多种方式的远程服务访问,兼顾性能和跨平台的不同需求,因此在原有的WebService的SCA发布及绑定的功能外,集成了Hessian,用以发布和访问内部服务(安全性和跨平台要求不高,性能要求较高的服务)...
它以基于流行的JEE开源技术整合,以JBPM4.4流程引擎为基础,采用了WebService、XML、J2EE、Spring组件的灵活配置,并且与Microsoft Office实现了有机整合。完善了用户管理和安全的权限管理,支持POP3/POP3,SMTP/...
2.1记费管理:支持点播、包月、免费等多种记费方式的管理 2.2用户帐号管理:支持Windows的分组无级权限关联 2.3业务逻辑管理:支持业务的定制、取消、发送方式、定时以及扣费方式 2.4内容管理:对各业务对应的内容...
前几天,工作上有一新需求,需要前端web页面异步调用后台的Webservice方法返回信息。实现方法有多种,本例采用jQuery+Ajax,完成后,在本地调试了一切ok,但是部署到服务器上以后就出现问题了,后台服务调用没有响应...
数据交换平台包含的数据交换服务可以发布为多种形式,包括:EJB、Servlet、WebService等 数据交换平台的数据,使用XML格式进行表示 数据交换平台要求提供安全认证和授权访问机制,确保数据交换的安全
前几天,工作上有一新需求,需要前端web页面异步调用后台的Webservice方法返回信息。实现方法有多种,本例采用jQuery+Ajax,完成后,在本地调试了一切ok,但是部署到服务器上以后就出现问题了,后台服务调用没有响应...
9.3.3 主页面与登录相关的WebService数据访问模块 9.3.4 发表主题相关的Web Service访问模块 9.3.5 投票相关的Web Service访问模块 9.3.6 管理相关的Web Service访问模块 9.4 主页面与登录模块 ...