http://username:password@example.com出于安全原因,某些应用程序和操作系统不再支持将用户名和密码()嵌入url中,因为这不是执行HTTP身份验证的标准方法。Unity或Android很可能没有在自己这边实现。
我使用内置的Android浏览器对此进行了测试
http://Administrator:ZZh7y6dn@*IPAddress*:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/,但无法正常运行。所以,我想这个问题来自Android。
我再次测试时没有用户名和密码,
http://*IPAddress**:8080/Thingworx/Things/SimulationData/Properties/OvenTemperature/然后出现了登录窗口。当我输入用户名和密码时,它起作用了。
您仍然可以使用
UnityWebRequest通过提供解决这一问题
AUTHORIZATION头到
UnityWebRequest与
SetRequestHeader功能。仅当授权类型
Basic为时,此方法才有效
Digest。您的情况是
HTTPBasic。
对于一般解决方案:
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(); .......}对于您的问题的解决方案:
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"; } } } }}


