用于HTTP基本身份验证的UnityWebRequest嵌入用户+密码数据在Android上不起作用 [英] UnityWebRequest Embedding User + Password data for HTTP Basic Authentication not working on Android

查看:86
本文介绍了用于HTTP基本身份验证的UnityWebRequest嵌入用户+密码数据在Android上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码用于从我们自己的系统之一中托管的Thingworx服务器获取温度值. 这在整体上非常好用. 但不在andoird 中,生成apk后,它将不会从服务器获取任何数据,并且将建立连接.但是,它只是不会获取数据并将其放入文本网格中.

The Code below is used to get Temperature Value from Thingworx server that is hosted in one of our own systems. This works perfectly well in unity. But not in andoird, once apk is generated, it won't fetch any data from the server and there will be connection established. But, it just wont fetch the data and put that into the text mesh.

我正在使用unity 5.4.1 32bit.签入Android-5.0.2和6.

I'm using unity 5.4.1 32bit . Check in both Android - 5.0.2 and 6.

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Text.RegularExpressions;
using System;
using UnityEngine.UI;

public class  GETTempValue : MonoBehaviour {


public GameObject TempText;
static string TempValue;

void Start() 
{
    StartCoroutine(GetText());
}

IEnumerator GetText() 
{
    Debug.Log("Inside Coroutine");
    while (true) 
    {
        yield return new WaitForSeconds(5f);
        string url = "http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/";

        Debug.Log("Before UnityWebRequest");
        UnityWebRequest www = UnityWebRequest.Get (url);
        yield return www.Send();
        Debug.Log("After UnityWebRequest");
        if (www.isError) {
            Debug.Log ("Error while Receiving: "+www.error);
        } else {
            Debug.Log("Success. Received: "+www.downloadHandler.text);
            string result = www.downloadHandler.text;
            Char delimiter = '>';

            String[] substrings = result.Split(delimiter);
            foreach (var substring in substrings) 
            {
                if (substring.Contains ("</TD")) 
                {
                    String[] Substrings1 = substring.Split ('<');
                    Debug.Log (Substrings1[0].ToString()+"Temp Value");
                    TempValue = Substrings1 [0].ToString ();
                    TempText.GetComponent<TextMesh> ().text = TempValue+"'C";
                }   
            }
        }

    }

}

}

这是android清单权限

this is the android manifest permission

uses-permission android:name="android.permission.INTERNET" 
uses-permission android:name="android.permission.CAMERA"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

推荐答案

出于安全原因,某些应用程序和操作系统不再支持在URL中嵌入用户名和密码(http://username:password@example.com).这是因为这不是标准方式执行HTTP身份验证. Unity或Android很可能没有一面实现这一目标.

Embedding username and password(http://username:password@example.com) in a url is no longer supported in some Applications and OS for security reasons.That's because this is not the standard way to perform HTTP Authentication. It very likely that Unity or Android did not implement this on the their side.

我使用http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/在内置的Android浏览器中对此进行了测试,但无法运行.所以,我想这个问题是来自Android.

I tested this on the built-in Android Browser with http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/ and it failed to function. So, I guess this problem is from Android.

我再次测试没有用户名和密码http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/,然后出现了登录窗口.输入用户名和密码后,它就起作用了.

I tested again without username and password http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/ then the login window appeared. When I entered the username and password, it worked.

您仍然可以使用UnityWebRequest来解决此问题,方法是使用SetRequestHeader函数为UnityWebRequest提供AUTHORIZATION标头.仅当授权类型为Basic而不是Digest时,此方法才有效.您的情况是HTTP Basic.

You can still use UnityWebRequest to solve this problem by providing the AUTHORIZATION header to the UnityWebRequest with the SetRequestHeader function. This will only work if the authorization type is Basic instead of Digest. In your case, it is HTTP Basic.

对于一般解决方案:

string authenticate(string username, string password)
{
    string auth = username + ":" + password;
    auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
    auth = "Basic " + auth;
    return auth;
}

IEnumerator makeRequest()
{
    string authorization = authenticate("YourUserName", "YourPassWord");
    string url = "yourUrlWithoutUsernameAndPassword";


    UnityWebRequest www = UnityWebRequest.Get(url);
    www.SetRequestHeader("AUTHORIZATION", authorization);

    yield return www.Send();
    .......
}

对于您问题的解决方案:

For solution in your question:

public GameObject TempText;
static string TempValue;

void Start()
{
    StartCoroutine(GetText());
}

string authenticate(string username, string password)
{
    string auth = username + ":" + password;
    auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
    auth = "Basic " + auth;
    return auth;
}

IEnumerator GetText()
{
    WaitForSeconds waitTime = new WaitForSeconds(2f); //Do the memory allocation once

    string authorization = authenticate("Administrator", "ZZh7y6dn");
    while (true)
    {
        yield return waitTime;
        string url = "http://*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/";


        UnityWebRequest www = UnityWebRequest.Get(url);
        www.SetRequestHeader("AUTHORIZATION", authorization);
        yield return www.Send();

        if (www.isError)
        {
            Debug.Log("Error while Receiving: " + www.error);
        }
        else
        {
            string result = www.downloadHandler.text;
            Char delimiter = '>';

            String[] substrings = result.Split(delimiter);
            foreach (var substring in substrings)
            {
                if (substring.Contains("</TD"))
                {
                    String[] Substrings1 = substring.Split('<');
                    Debug.Log(Substrings1[0].ToString() + "Temp Value");
                    TempValue = Substrings1[0].ToString();
                    TempText.GetComponent<TextMesh>().text = TempValue + "'C";
                }
            }
        }
    }
}

这篇关于用于HTTP基本身份验证的UnityWebRequest嵌入用户+密码数据在Android上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆