Blazeds RPC调用 之 HttpService

使用HttpService 调用后台分为两种情况

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

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

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

var httpService1:HTTPService = new HTTPService();
httpService1.url = "http://www.baidu.com";
httpService1.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
httpService1.addEventListener(FaultEvent.FAULT,onFalut);
httpService1.addEventListener(ResultEvent.RESULT,onResult);
httpService1.send();

 只要你赋上url属性并且设置下resultFormat的格式,就可以直接发送了。

第二种,稍微比较麻烦一些。

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

<destination id="DefaultHTTP">
	<properties>
	     <!-- The endpoint available to the http proxy service -->
	     <url>http://www.baidu.com</url>
	     <!-- Wildcard endpoints available to the http proxy services -->
	      <dynamic-url>http://*</dynamic-url>
	</properties>
</destination>

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

 <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
        </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 httpService2:HTTPService = new HTTPService();
httpService2.destination = "DefaultHTTP";
httpService2.url = "http://www.baidu.com";
httpService2.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
httpService2.useProxy = true;
httpService2.channelSet = channelSet;
httpService2.addEventListener(FaultEvent.FAULT,onFalut);
httpService2.addEventListener(ResultEvent.RESULT,onResult);
httpService2.send();

 你可以看到你要设置httpservice的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" xmlns:pivot="com.flexmonster.pivot.*">
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.messaging.ChannelSet;
			import mx.messaging.channels.HTTPChannel;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.http.HTTPService;
			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 httpService1:HTTPService = new HTTPService();
				httpService1.url = "http://www.baidu.com";
				httpService1.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
				httpService1.addEventListener(FaultEvent.FAULT,onFalut);
				httpService1.addEventListener(ResultEvent.RESULT,onResult);
				httpService1.send();
				
			}
			
			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 httpService2:HTTPService = new HTTPService();
				httpService2.destination = "DefaultHTTP";
				httpService2.url = "http://localhost:8400/blazeds/Test.html";
				httpService2.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
				httpService2.useProxy = true;
				httpService2.channelSet = channelSet;
				httpService2.addEventListener(FaultEvent.FAULT,onFalut);
				httpService2.addEventListener(ResultEvent.RESULT,onResult);
				httpService2.send("aaa");
				
			}
			
		]]>
	</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>

 

 

你可能感兴趣的:(service)