首页 > 解决方案 > 遇到无效的重定向。缺少位置标头。统一问题

问题描述

我正在使用 Unity WWWForm 将许可证验证请求发布到 URL。该代码在 Unity 5.6 中有效,但在 Unity 2017.3.1f1 中无效。我也在 Unity 2018 中尝试过。它没有用。

这是错误消息:遇到无效重定向(缺少位置标头?)

这是我正在使用的代码。

void Awake() {
    Instance = this;
    WWWForm form = new WWWForm ();
    mainHeader = form.headers;

    mainHeader ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes ("dummyId:#dummyPassword"));

}


public void HitForLicence(){
    string jsonData = JsonUtility.ToJson (new LicenseData { 

        LICENCE_KEY="kwsnfdksfksnf",
        MACHINE_IP="192.168.1.1"

    });




    Debug.Log (jsonData);

    byte[ ] postData = System.Text.Encoding.ASCII.GetBytes (jsonData);


    //headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes ("unity:@piun!ty"));
    if (mainHeader.ContainsKey ("Content-Type")) {

        mainHeader ["Content-Type"] = "application/json";

    } else {

        mainHeader.Add ("Content-Type", "application/json");
    }


    WWW www = new WWW (LicenseURL, postData, mainHeader);
    StartCoroutine (CheckForLicense (www));

}

public IEnumerator CheckForLicense (WWW www)
{

    Debug.Log("Check For License..");


    yield return www;


    //if (www.isDone ) {
        if (www.error != null) {
            ClearKeyBox ();
            print (www.error);
        }
        else {
            print (www.text);
            jsonNode = SimpleJSON.JSON.Parse(www.text);
            print ("MSG "+ jsonNode["MSG"].ToString());
        }
    //} 


    if (jsonNode != null && jsonNode["MSG"].Equals(ValidStr)) {
        HandleTextFile.WriteString(_SystemMACAddress+"-"+keyEntered);
        // Next screen
    } else {
        ClearKeyBox ();
    }

}

有人遇到过这个吗?请帮忙。

标签: unity3dserver

解决方案


WWW在 Unity 2018 下我也遇到了一些问题。

目前我正在使用UnityWebRequest- 我认为WWW已经过时,但我找不到任何参考。

基本上像这样更改您的代码:

...

    UnityWebRequest www = UnityWebRequest.Post(LicenseURL, form)
    StartCoroutine (CheckForLicense (www));

}

public IEnumerator CheckForLicense (WWW www)
{

    Debug.Log("Check For License..");

...

我认为您必须通过设置头部SetRequestHeader并将您的帖子数据放入 WWWForm ...

我现在无法检查它......希望它有帮助!


推荐阅读