[英]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.