flex中通过代码获取supermap的token

时间:2021-10-15 19:17:00

最近工作中需要使用代码来获取supermap服务启动安全访问限制以后的token值,经过一番尝试,最终成功获取到,记录下里,以供翻阅

//get token

public function getToken:void{

  var url:String = "http://IP地址/iserver/services/security/tokens.json";   //url地址最后要加.json!!!

  var variables:URLVariables = new URLVraiables();

  var request:URLRequest = new URLRequest();

  var loader:URLLoader = new URLLoader();

  var userName:String = "用户名";  //iserver上管理者的用户名

  var password:String = "密码";  //iserver上管理者的密码

  var clienttype:String = "Referer"  //选择获取令牌的方式  这里是通过地址获得

  var referer:String = "你需要启用安全访问限制的服务地址";

  var expiration:int = 60;    //令牌的时效  单位是分钟

  variables.userName = userName;

  variables.password = password;

  variables.clienttype = clientype;

  variables.referer = referer;

  variables.expiration = expiration;  //将数据塞入variables中

  request.url = url;

  request.method = URLRequestMethod.POST;

  request.data = JSON.stringify(variables);  //url请求中数据格一般都是json对象

  var requestHeader:URLRuestHeader = new URLRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");

  request.requestHeaders.push(requestHeader);

  loader.load(request);

  loader.addEventListener(Event.COMPLETE,resultHandler);

}

//使用cridential对象处理数据

public function resultHandler(e:Event):void{

  public static var CREDENTIAL:Credential = null;

  var loader:URLLoader = URLLoader(e.target);

  var obj:Object = loader.data;

  Credential.CREDENTIAL = new Credential(obj.toString());

  //在通过地址访问服务的时候就会在后面加上令牌了

}