2

我在 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"; 
    }
4

1 回答 1

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);
}
于 2013-12-06T11:01:47.980 回答