栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402867.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号