dongyao2129 2013-09-16 09:04
浏览 54
已采纳

ASP.net Adob​​e Air通讯,会话控制

Project: Adobe Air for Android with connection to the ASP.net. Here is AS3 code:

//First login to the server and also grabbing from there ASP_NET_SessionId data (Cookie).
var session:String;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;

var request:URLRequest= new URLRequest("https://somedomain.com/someaddress.aspx"); //yes with https
request.data = '{ "Message":"Login", "Data": { "User":"some@email.com", "Password":"somepassword" }}';
request.method = URLRequestMethod.POST;   

loader.addEventListener( HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpStatusHandler);   
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);

private function httpStatusHandler(e:HTTPStatusEvent):void 
{
    for each (var object:Object in e.responseHeaders)
    {
           if (object.name == "Set-Cookie")
           {
              var match:Array =object.value.match(/ASP.NET_SessionId=(.*?);/i);
              session = match[1];
              return
           }
    }
}

Next call is with session data (Cookie) I got already:

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY; //now I just need binary data

var request:URLRequest= new URLRequest("https://somedomain.com/someaddress.aspx"); //yes with https
request.data = '{ "Message":"DoSomeStaff", "Data": { "Some":"info"}}';   
request.method = URLRequestMethod.POST;
request.requestHeaders = new Array(new URLRequestHeader("Cookie", "ASP.NET_SessionId=" + session )); //addedd Cookie data here

loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);

And I'm losing session on server and can't get data (need to relogin)

Also I have this line in crossdomain.xml

allow-http-request-headers-from domain="*" headers="*"

ASP.net server side has no problem cause, the same kind of call from PHP works fine. Here it is:

$data_string ='{ "Message":"Login", "Data": { "User":"some@email.com", "Password":"somepassword" }}';
$ch = curl_init('https://somedomain.com/someaddress.aspx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json charset: UTF-8','Content-Length: ' . mb_strlen($data_string)));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER ,1);
$result = curl_exec($ch);
curl_close($ch);

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
$sessionId = $cookies["ASP_NET_SessionId"];

In the next call I'm just using that session variable:

curl_setopt($ch, CURLOPT_COOKIE, "ASP.NET_SessionId=$sessionId; path=/");

And everything works fine.

My question is: what I'm missing in my Actionscript code? Or what might be a problem?

  • 写回答

1条回答 默认 最新

  • duanjiao7440 2013-09-16 19:07
    关注

    Have you checked whether your app actually received the correct cookie data? And also whether the expected cookie header is actually being received by your server?

    In my experience AIR and cookies has always been pretty funky on different OS/devices. However at least in the emulator AIR handles cookie retrieval/sending by itself just fine (during the application lifecycle), so you could give that a try in order to isolate the problem further.

    Theoretically it shouldn't be necessary, but also try setting request.manageCookies to false (it's at least required on Windows) to make sure that AIR actually lets you set possible cookie headers that it would otherwise define by itself.

    btw, crossdomain restrictions apply to browser content only, not to apps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊