---
title: "Flash访问webservice服务的跨域解决方案"
author: "can4you"
date: "2010-12-29T22:44:45Z"
tags: ["ActionScript", "Flash", "Tomcat", "WebService", "XML"]
categories: ["企业架构"]
slug: "flash-webservice"
draft: false
lastmod: "2011-01-07T08:46:25Z"
id: 855298
url: "https://can4you.iteye.com/blog/855298"
---

上次系统平台嵌套FlashGIS地图的时候，也出现过跨域访问不了的问题

[http://can4you.iteye.com/blog/829419](/blog/829419)

不过那次问题是跨域访问不同服务器上的FlashGIS Framework文件资源（如js,swf等静态文件），因为资源文件时部署在TOMCAT下的，所以问题的解决方案是在TOMCAT目录：webapps/ROOT/下放置一个crossdomain.xml文件，其内容为：

 

```
<?xml version="1.0"?>
<cross-domain-policy>
  <allow-access-from domain="*" />
</cross-domain-policy>
```

  

而这次客户端flash在actionscript脚本中通过调用系统提供的webservice服务来完成它们之间的交互，获取flash上的路径坐标，上传到后台处理，部分actionscript脚本如下：

 

```
function update(imageId:String, index:String) {
	//构建WebService类，传入服务的自述地址
	var ImagePathService:WebService = new WebService("http://localhost:8080/myproj/services/imagePathService?WSDL");
	//接收服务的返回结果
	ImagePathService.saveNewPoints(imageId, index);
	ResultObj = ImagePathService.update(imageId, index);
	//判断返回结果并执行操作，onResult监测返回成功事件
	ResultObj.onResult = function(result:Boolean) {
		//返回值（由web服务返回类型决定）
		if (result == true) {
			Alert.show("上传成功！");
		} else {
			Alert.show("上传失败！");
		}
	};
	//判断返回结果并执行操作，onFault监测返回失败事件
	ResultObj.onFault = function(result) {
		Alert.show("上传失败");
	};
}

```

 

 

虽然在Tomcat目录下的webapps/ROOT/下已经存在crossdomain.xml文件，但是还是出现跨域访问不了的问题，无法调用webservice，百思不得其解，在网上找了一下，发现在crossdomain.xml中修改内容可以很好的解决，最后修改如下：

 

```
<?xml version="1.0"?>
<cross-domain-policy>
  <site-control permitted-cross-domain-policies="all" /> 
  <allow-access-from domain="*" />
  <allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
```

   

相比之前的文件，就增加了&lt;site-control和&lt;allow-http-request-headers-from这两个tag节点，其中前一个不是必须的，但是如果少了&lt;allow-http-request-headers-from节点的话，调用webservice就不行，问题就出在这，参考一下文章找出的解决方案：

[http://blog.csdn.net/lena\_20081215/archive/2009/01/21/3849009.aspx](http://blog.csdn.net/lena_20081215/archive/2009/01/21/3849009.aspx)

 

解析：

1.  WEB服务器是TOMCAT，所以crossdomain.xml文件是放置在根目录即webapps/ROOT/下

2.  crossdomain.xml的内容配置和FlashPlayer的版本有关，在9版本之前，通过第一种配置是可以的，但是在版本9和之后的版本对安全策略有着更严格的要求，尤其是对webservice的调用

3.  如果安全策略文件放在根目录，文件名字就必须为crossdomain.xml，这都有严格要求；如果不放置在根目录，需要通过System.security.loadPolicyFile(xml的路径)来加载这个文件

4.  这次主要问题是安全策略的问题，以后如果再涉及到跨越问题时，解决问题就省事多了

 

 

其他参考文章：

[http://hi.baidu.com/wosinmaiken/blog/item/637a6e4b998a49fe82025c85.html](http://hi.baidu.com/wosinmaiken/blog/item/637a6e4b998a49fe82025c85.html)

[http://hi.baidu.com/lotusxyhf/blog/item/f1c08a58b5771e88810a1870.html/cmtid/1b4b1036c0ea62d3a3cc2bf2](http://hi.baidu.com/lotusxyhf/blog/item/f1c08a58b5771e88810a1870.html/cmtid/1b4b1036c0ea62d3a3cc2bf2)