Blazeds RPC调用 之 webService

和HttpService类似,调用后台有两种方式,

1.直接访问wsdl服务而不用通过blazeds服务端的代理服务

2.通过blazeds服务端的代理服务

先说第一种,就比较简单。

var webService:WebService = new WebService();
webService.wsdl = "http://feeds.adobe.com/webservices/mxna2.cfc?wsdl";
webService.addEventListener(FaultEvent.FAULT,onFalut);
webService.addEventListener(ResultEvent.RESULT,onResult);
webService.loadWSDL();
webService.getCategories();

 只要你赋上wsdl属性,并且调用loadWSDL() (此方法在mxml标签中会自动调用) ,然后调用你要访问的operation的名字即可。

第二种,稍微麻烦一些

首先,现在proxy-config.xml 文件中设置: 如图1

<destination id="ws-catalog">
	<properties>
		<wsdl>http://feeds.adobe.com/webservices/mxna2.cfc?wsdl</wsdl>
		<soap>*</soap>
	</properties>
	<adapter ref="soap-proxy"/>
</destination>

 然后,确保services-config.xml中有http的channel定义。如图2

Xml代码 
  1. <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">  
  2.            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>  
  3.        </channel-definition>  

 最后编写flex代码

var channelSet:ChannelSet = new ChannelSet();
var httpChannel:HTTPChannel = new HTTPChannel("my-polling-amf", "http://localhost:8400/blazeds/messagebroker/http");
channelSet.addChannel(httpChannel);
var webService:WebService = new WebService();
webService.destination = "ws-catalog";
webService.useProxy = true;
webService.channelSet = channelSet;
webService.addEventListener(FaultEvent.FAULT,onFalut);
webService.addEventListener(ResultEvent.RESULT,onResult);
webService.loadWSDL();
webService.getCategories();

 你可以看到你要设置webService的destination,channelSet并设置useProxy为true.

最终的app 文件为:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="955" minHeight="600">
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.messaging.ChannelSet;
			import mx.messaging.channels.AMFChannel;
			import mx.messaging.channels.HTTPChannel;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.http.HTTPService;
			import mx.rpc.soap.WebService;
			private function onFalut(event:FaultEvent):void
			{
				trace("onFalut");
				Alert.show("onFalut");
			}
			
			private function onResult(event:ResultEvent):void
			{
				trace("onResult");
				Alert.show("onResult");
			}
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				var webService:WebService = new WebService();
				webService.wsdl = "http://feeds.adobe.com/webservices/mxna2.cfc?wsdl";
				webService.addEventListener(FaultEvent.FAULT,onFalut);
				webService.addEventListener(ResultEvent.RESULT,onResult);
				webService.loadWSDL();
				webService.getCategories();
				
			}
			
			protected function button2_clickHandler(event:MouseEvent):void
			{
				var channelSet:ChannelSet = new ChannelSet();
				var httpChannel:HTTPChannel = new HTTPChannel("my-polling-amf", "http://localhost:8400/blazeds/messagebroker/http");
				channelSet.addChannel(httpChannel);
				var webService:WebService = new WebService();
				webService.destination = "ws-catalog";
				webService.useProxy = true;
				webService.channelSet = channelSet;
				webService.addEventListener(FaultEvent.FAULT,onFalut);
				webService.addEventListener(ResultEvent.RESULT,onResult);
				webService.loadWSDL();
				webService.getCategories();
				
			}
			
		]]>
	</fx:Script>
	<s:VGroup>
		<s:Button label="Directly access" click="button1_clickHandler(event)"/>
		<s:Button label="Pass by proxy" click="button2_clickHandler(event)"/>
	</s:VGroup>
</s:Application>

 

你可能感兴趣的:(webservice,blazeds)