新浪UED

关于Swf文件发出的请求能否得到referer和cookie的测试

by seamoon / 前端开发,博客,推荐 / 2010-12-16
关于Swf文件发出的请求能否得到referer和cookie的测试

最近由于项目需要,在没有完全开放crossdomain.xml的前提下需要知道从Swf文件发出的请求在能否被服务器得到与该请求相关http header 头里面的referer和cookie。

测试工具:Fiddler,Firebug,Arthropod.

测试方法:让swf以以下的几种方式向服务器发送请求,用fiddler或者firebug监听该请求的http header信息。

测试环境:
系统:window 7;
浏览器:IE8, Firefox 3.6.12,Chrome 7.0.517.41, Safari 5.0.2,Opera 10.63;
Flash Player : 10.1.85.3 ; 9.0.280.0;

请求一个地址;请求方式:GET;
两种版本的Player结果一样:

IE8 Firefox chrome safari opera
Referer Yes No Yes No Yes
cookie Yes Yes Yes Yes Yes

请求一个地址;请求方式:POST;
两种版本Player的结果一样:

IE8 Firefox chrome safari opera
Referer Yes Yes Yes Yes Yes
cookie Yes Yes Yes Yes Yes

利用ByteArray方式上传图片;请求方式:POST;
请求头信息:(“Content-type”, “application/octet-stream”);
两种版本的Player结果一样:

IE8 Firefox chrome safari opera
Referer Yes Yes Yes Yes Yes
cookie Yes Yes Yes No Yes

利用FileReference 上传图片;请求方式:POST;
两种版本的Player结果一样:

IE8 Firefox chrome safari opera
Referer No No No No No
cookie Yes No No No No

以上的非IE浏览器,在用FileReference上传图片时,如果ie里面有cookie,则它们会得到ie里面的cookie!

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.DataEvent;

	import flash.net.URLLoader;
	import flash.net.URLRequestMethod;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.FileReference;
	import flash.net.FileFilter;

	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Bitmap;
	import flash.display.BitmapData;

	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	import com.carlcalderon.arthropod.Debug;
	import com.adobe.images.JPGEncoder;
	import SimpleBtn;

	/**
	 * ...
	 * @author Rialover
*/
	public class Main extends Sprite
	{
		private var _url:String = "http://www.test.com/request_test/request.php";
		private var _picURL:String = "blog.jpg";
		private var _uploadURL:String = "http://www.upload.com/upload2.php";
		private var _req:URLRequest;
		private var _loader:URLLoader;
		private var _bmd:BitmapData;
		private var _btn:SimpleBtn;
		private var _fileRef:FileReference;
		private var _imgFilter:FileFilter;

		public function Main():void
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);

			_req = new URLRequest(_url);
			//_req.method = URLRequestMethod.POST;
			var val:URLVariables = new URLVariables();
			val["name"] = "seamoon";
			val["age"] = 26;
			_req.data = val;

			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, dataOK);
			_loader.load(_req);

			_btn = new SimpleBtn(" 浏 览 ");
			_btn.x = _btn.y = 30;
			_btn.addEventListener(MouseEvent.CLICK, selectLocalPic);
			addChild(_btn);

			_fileRef = new FileReference();
			_imgFilter = new FileFilter("Image Files (*.jpg, *.gif, *.jpeg, .*.png)", "*.jpg; *.gif; *.jpeg; *.png");
		}

		private function dataOK(evt:Event):void
		{
			var str:String = _loader.data as String;
			Debug.log("text request Hander",0xFF3300);
			Debug.log(str,0xFFFFFF);
			//loaderPic(_picURL);
		}

		/**
		 * 加载初始头像
		 * @param	url
		 */
		public function loaderPic(url:String):void
		{
			var picReq:URLRequest = new URLRequest(url);
			var picLoader:Loader = new Loader();
			var lc:LoaderContext = new LoaderContext(true);
			picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, initPicHandler);
			picLoader.load(picReq,lc);
		}

		/**
		 * 初始头像加载完成
		 * @param	evt
		 */
		private function initPicHandler(evt:Event):void
		{
			var tgt:LoaderInfo = evt.target as LoaderInfo;
			tgt.removeEventListener(Event.COMPLETE, initPicHandler);

			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, initBMD);
			loader.loadBytes(tgt.bytes);
		}

		/**
		 * 得到初始头像的 BitmapData
		 * @param	evt
		 */
		private function initBMD(evt:Event):void
		{
			var tgt:LoaderInfo = evt.target as LoaderInfo;
			tgt.removeEventListener(Event.COMPLETE, initBMD);
			var loader:Loader = tgt.loader as Loader;
			var bmd:BitmapData = new BitmapData(loader.width, loader.height);
			bmd.draw(loader);
			_bmd = bmd;
			loader.unload();

			uploadPic();
		}

		private function uploadPic():void
		{
			var jpgEncoder:JPGEncoder = new JPGEncoder(100);
			var avatarBta:ByteArray = jpgEncoder.encode(_bmd);

			var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
			var jpgUploadRequest:URLRequest = new URLRequest(_uploadURL + "?name=" + new Date().getTime() + ".jpg");

			jpgUploadRequest.requestHeaders.push(header);
			jpgUploadRequest.method = URLRequestMethod.POST;
			jpgUploadRequest.data = avatarBta;

			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, uploadComplete);
			loader.load(jpgUploadRequest);
		}

		private function uploadComplete(evt:Event):void
		{
			Debug.log("upload jpg Hander",0xFF3300);
			var loader:URLLoader = evt.target as URLLoader;
			Debug.log(loader.data,0xFFFFFF);
		}

		private function selectLocalPic(evt:MouseEvent):void
		{
			_fileRef.browse([_imgFilter]);
			_fileRef.addEventListener(Event.SELECT, onFileSelected);
		}

		/**
		 * 根据FP的版本,选择如何处理本机图片
		 * @param	evt
		 */
		private function onFileSelected(evt:Event):void
		{
			_fileRef.removeEventListener(Event.SELECT, onFileSelected);
			uploadFile();
		}

		private function uploadFile():void
		{
			var urlrequest:URLRequest = new URLRequest("http://www.upload.com/upload/uploadPic.php");
			_fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, startPhotoCut);
			_fileRef.upload(urlrequest);
		}

		private function startPhotoCut(evt:DataEvent):void
		{
			Debug.log(evt.toString());
		}

	}

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

* 验证图片 刷新验证码

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>