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

查看:29
本文介绍了UnityWebRequest 嵌入用户 + 密码数据用于 HTTP 基本身份验证不适用于 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";
                }   
            }
        }

    }

}

}

这是安卓清单权限

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"

推荐答案

Embedding username and password(http://username:password@example.com) in a url is no longer supported in a url出于安全原因,应用程序和操作系统.那是因为这不是执行 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 地址**: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 来解决此问题,方法是将 AUTHORIZATION 标头与 SetRequestHeader 一起提供给 UnityWebRequest> 功能.这仅在授权类型是 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";
                }
            }
        }
    }
}

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

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