出于安全原因,某些应用程序和操作系统不再支持在URL中嵌入用户名和密码(http:// username:password@example.com).这是因为这不是执行HTTP身份验证的标准方法. Unity或Android很可能没有在他们身边实现这一点.
我在内置的Android浏览器上使用http:// Administrator:ZZh7y6dn @ * IP Address *:8080 / Thingworx / Things / SimulationData / Properties / OvenTemperature /进行了测试,但无法正常运行.所以,我猜这个问题来自Android.
我再次测试没有用户名和密码http:// * IP地址**:8080 / Thingworx / Things / SimulationData / Properties / OvenTemperature /然后出现登录窗口.当我输入用户名和密码时,它有效.
您仍然可以使用UnityWebRequest通过使用SetRequestHeader函数向UnityWebRequest提供AUTHORIZATION标头来解决此问题.这仅在授权类型为“基本”而不是“摘要”时才有效.在您的情况下,它是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();
.......
}
对于您的问题的解决方案:
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("
{
String[] Substrings1 = substring.Split('
Debug.Log(Substrings1[0].ToString() + "Temp Value");
TempValue = Substrings1[0].ToString();
TempText.GetComponent().text = TempValue + "'C";
}
}
}
}
}