본문 바로가기

prev record

[2010.05.14.금요일] URLVariable

URLVariables는 응용 프로그램과 서버 사이에 변수를 전송할 수 있게 해준다. URLRequest 클래스의 data 속성에URLVariables 객체를 사용하면 된다.

사용법은 정말 간단하다. 아래와 같이 쓰면 된다.

  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.net.navigateToURL;
  4.     import flash.net.URLRequest;
  5.     import flash.net.URLVariables;
  6.  
  7.     public class URLVariablesExample extends Sprite {
  8.  
  9.         public function URLVariablesExample() {
  10.             var  loader:URLLoader;
  11.             var url:String = http://www.Example.com/application.php;
  12.             var request:URLRequest = new URLRequest(url);
  13.             var variables:URLVariables = new URLVariables();
  14.             request.method = URLRequestMethod.POST;
  15.             variables.exampleSessionId = new Date().getTime();
  16.             variables.exampleUserLabel = "guest";
  17.             request.data = variables;
  18.            loader .load(request);
  19.         }
  20.     }
  21. }


    URLRequest의 인스턴스 변수 variables에 동적으로 변수 값을 할당해서 보내 주면된다. 위의 15, 16 줄 같은 경우 exampleSessionId와 exampleUserLabel 변수가 application.php 에서 변수로 있으면 이값을 줄수가 있고, php에서 이값들을 받고 일련의 명령들을 처리해서 결과 xml등을 웹에서 뿌려주면 URLLoader의 인스턴스 변수 loader가 그것을 로드하게 된다.

    여기서 중요한 것은 14 줄이다. php는 post 방식과  get 방식으로 값을 전달 받을 수 있다. 지금과 같은 경우 post 방식일때 사용하는 것이다. 이 경우 14 줄이 꼭 있어야 값이 전달된다.

    get 방식일 경우는 url에서 ?id 로 직접 접근하면된다. 예를 들어,

    http://www.Example.com/application.php?exampleSession