以多种方式访问WebService

本文详细介绍了Java和js访问WebService的多种方法,包括使用Axis、XFire、ActiveXObject和jQuery的ajax进行调用,并提供了Oracle中调用WebService的示例。主要内容涉及WebService的调用、数据传递和解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[b]一、Java访问WebService[/b]
[b](1)使用Axis[/b]

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();
}
}
}

[b](2)使用XFire[/b]

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());
}
}

[b]二、js访问WebService[/b]
[b](1)使用ActiveXObject[/b]

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);
}

[b](2)使用jQuery的ajax[/b]

$(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);
}
});

});

[b]三、在oracle中调用WebService[/b]
[b](1)使用UTL_HTTP[/b]

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;

[b](2)使用UTL_DBWS[/b]
例子:[url=http://www.oracle-base.com/articles/10g/utl_dbws10g.php]Consuming Web Services in Oracle 10g[/url]
说明:oracle自己的例子成功了,调用GetWeather的WebService,没成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值