android u3d验证,c# – UnityWebRequest嵌入HTTP基本身份验证的用户密码数据不适用于Android...

出于安全原因,某些应用程序和操作系统不再支持在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";

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值