简体   繁体   English

用于HTTP基本身份验证的UnityWebRequest嵌入用户+密码数据在Android上不起作用

[英]UnityWebRequest Embedding User + Password data for HTTP Basic Authentication not working on Android

The Code below is used to get Temperature Value from Thingworx server that is hosted in one of our own systems. 以下代码用于从我们自己的系统之一中托管的Thingworx服务器获取温度值。 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. 但是不在andoird中 ,一旦生成了apk,它就不会从服务器获取任何数据,并且会建立连接。 But, it just wont fetch the data and put that into the text mesh. 但是,它只是不会获取数据并将其放入文本网格中。

I'm using unity 5.4.1 32bit . 我正在使用unity 5.4.1 32bit。 Check in both Android - 5.0.2 and 6. 检入Android-5.0.2和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";
                }   
            }
        }

    }

}

}

this is the android manifest permission 这是android清单权限

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 some Applications and OS for security reasons.That's because this is not the standard way to perform HTTP Authentication. 出于安全原因,某些应用程序和操作系统不再支持将用户名和密码( http://username:password@example.com )嵌入url中,因为这不是执行HTTP身份验证的标准方法。 It very likely that Unity or Android did not implement this on the their side. Unity或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. 我在内置的Android浏览器中使用http://Administrator:ZZh7y6dn@*IP Address*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/ ,但无法运行。 So, I guess this problem is from Android. 所以,我想这个问题来自Android。

I tested again without username and password http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/ then the login window appeared. 我再次测试时没有用户名和密码http://*IP Address**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/然后出现了登录窗口。 When I entered the username and password, it worked. 当我输入用户名和密码时,它起作用了。

You can still use UnityWebRequest to solve this problem by providing the AUTHORIZATION header to the UnityWebRequest with the SetRequestHeader function. 您仍然可以通过使用SetRequestHeader函数向UnityWebRequest提供AUTHORIZATION标头来使用UnityWebRequest解决此问题。 This will only work if the authorization type is Basic instead of Digest . 仅当授权类型为Basic而不是Digest此方法才有效。 In your case, it is HTTP Basic . 在您的情况下,它是HTTP Basic

For general solution: 对于一般解决方案:

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";
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnityWebRequest 向 HTTP 发送空白数据 - UnityWebRequest sending blank data to HTTP 更新Android操作系统后UnityWebRequest无法正常工作 - UnityWebRequest not working after updating Android OS 使用 C# 中的 HTTP 基本身份验证将用户重定向到页面 - Redirect user to a page with HTTP Basic Authentication in C# 无法使用Unity中的UnityWebRequest从HTTP响应中获取数据 - Cannot get data from HTTP Response using UnityWebRequest in Unity IIS中的WCF,http基本身份验证 - Windows用户“安全”的含义 - WCF in IIS, http basic authentication - Windows User “security” implications 如何将用户重定向到其他服务器并包含HTTP基本身份验证凭据? - How to redirect a user to a different server and include HTTP basic authentication credentials? 使用Xamarin Android通过http基本身份验证下载文件 - Download a file through http basic authentication with Xamarin Android 基本身份验证的用户名和密码不正确 - Incorrect username and password with Basic Authentication Unity WebGL UnityWebRequest 在上传和下载数据时不起作用 - Unity WebGL UnityWebRequest not working when uploading and downloading data 使用 UnityWebRequest 从 API 端点接收 JSON 数据不起作用 - Using UnityWebRequest to receive JSON data from API endpoint not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM