gpt4 book ai didi

unity-game-engine - UnityWebRequest 改为 https

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:02 28 4
gpt4 key购买 nike

我在 http 下有 unity android app 和 site api 的工作基础设施。

最近换了服务器,申请了ssl证书。现在我的 api 在 https 下。

在 unity 应用程序中,我使用 UnityWebRequest 与我的 api 进行通信。切换到 https 后的逻辑更改是将应用程序中的所有 api 地址从 http 更改为 https。我这样做了,但我的 api 表现得很奇怪。 (一直给出我自己的错误状态作为响应,而在没有证书的旧服务器上给出良好的响应。)

切换到 https 后,我还需要更改什么吗?

最佳答案

通常 Unity 会自动处理证书并根据已知的根证书对其进行验证或完全忽略它们,具体取决于平台:

UnityWebRequest.certificateHandler:
Setting this property to null makes the platform use the default certificate validation. Some platforms will validate certificates against a root certificate authority store. Other platforms will simply bypass certificate validation completely.

但是,如果 Unity 决定首先使用自签名证书,将会失败。


因此,对于带有自签名证书的 https,您可能必须实现 CertificateHandler实现方法 ValidateCertificate

您可以通过所有 接受它们来简单地绕过证书(这更容易,但当然会使 https 变得毫无意义)

public class BypassCertificate : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
//Simply return true no matter what
return true;
}
}

或者使用您的公钥从文档中尝试这个示例

// Based on https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#.Net
class AcceptAllCertificatesSignedWithASpecificPublicKey : CertificateHandler
{
// Encoded RSAPublicKey
private static string PUB_KEY = "30818902818100C4A06B7B52F8D17DC1CCB47362" +
"C64AB799AAE19E245A7559E9CEEC7D8AA4DF07CB0B21FDFD763C63A313A668FE9D764E" +
"D913C51A676788DB62AF624F422C2F112C1316922AA5D37823CD9F43D1FC54513D14B2" +
"9E36991F08A042C42EAAEEE5FE8E2CB10167174A359CEBF6FACC2C9CA933AD403137EE" +
"2C3F4CBED9460129C72B0203010001";

protected override bool ValidateCertificate(byte[] certificateData)
{
X509Certificate2 certificate = new X509Certificate2(certificateData);

string pk = certificate.GetPublicKeyString();

return pk.Equals(PUB_KEY));
}
}

并将其添加到您的请求中

using(var www = UnityWebRequest.Get("https://example.com"))
{
//www.certificateHandler = new BypassCertificate();
// Or
www.certificateHandler = new AcceptAllCertificatesSignedWithASpecificPublicKey();

yield return www.SendWebRequest();

//...
}

Note: Custom certificate validation is currently only implemented for the following platforms - Android, iOS, tvOS and desktop platforms.

所以在 Android 上你应该没问题。

CertificateHandler 默认为 automatically disposedUnityWebRequest 一起,所以没有更多的事情要做。

关于unity-game-engine - UnityWebRequest 改为 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51880156/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com