如何从统一的网页获取cookie标题数据?

时间:2013-11-08 09:06:46

标签: cookies login unity3d

我在unity3d和我的网页上有注册和登录页面,当我将我的注册或登录数据从unity3d发送到我的网站(magento cms)时,发送给我自己的错误“必要的数据倒带是不可能的”之后在HttpAnalyzer应用程序中查看我的请求我发现unity3d无法接受获取cookie字符串。如何在登录或注册用户后从网上获取cookie? 我的unity3d代码:

    var form = new WWWForm();
    form.AddField( "SN", SystemInfo.deviceUniqueIdentifier.ToString());
    form.AddField( "UserName ", UserName );
    form.AddField( "year",Year );
    form.AddField( "month", Month );
    form.AddField( "day", Day );
    form.AddField( "dob",Year+"/"+Month+"/"+Day );
    form.AddField( "gender", Gender );
    form.AddField( "password", Pass );
    // Create a download object
    var download = new WWW(URL, form);
    // Wait until the download is done
    yield download;
    // show the Results
    Debug.Log(download.text);   
    if(download.error != null)
    {
        Debug.Log("Err :"+download.error);
        Flag = "1"; 
    }

1 个答案:

答案 0 :(得分:2)

当您尝试加载的页面进行重定向时,通常会得到“无法进行必要的数据重绕”。我不认为您可以在Unity中使用“跟随重定向”选项发出请求。

我不知道洋红色的cms所以我不能告诉你如何纠正这个问题。 对于你的cookie问题,这在Unity中非常简单:你只需要在第一次调用时拦截标题“SET-COOKIE”,然后在下次调用时发回带有“Cookie”的标题:

#pragma strict

import System.Collections.Generic;

var url:String = "";
var cookie:String = "";

/// interface
private var stringToEdit:String = "";
function OnGUI(){
    if (url!="" && GUI.Button(Rect(10,10,200,30),"Click"))
        StartCoroutine(LoadData());

    stringToEdit = GUI.TextArea (Rect (10, 50, 600, 400), stringToEdit);
}
/// debug
function Debug(s:String){
    stringToEdit += s+"\n";
}

function LoadData():IEnumerator{
    Debug(url);

    var form : WWWForm = new WWWForm();

    var time:String     = System.DateTime.Now.Ticks.ToString();
    form.AddField("time", time);

    // construct your header calls
    var headers : Hashtable = form.headers;
    if(cookie!="")
        headers["Cookie"] = cookie;

    var www : WWW = new WWW(url, form.data, headers);

    yield www;

    if(!www.error){
        Debug(www.text);

        // get the cookie and keep it
        if(www.responseHeaders.ContainsKey('SET-COOKIE')){
            var data:String[] = www.responseHeaders['SET-COOKIE'].Split(";"[0]);
            if(data.length>0){
                cookie = data[0];
            }
        }

    }else
        Debug(www.error);
    // debug
    for(var header:KeyValuePair.<String, String> in www.responseHeaders)
        Debug(header.Key+" "+header.Value);
}