private static AndroidJavaObject sHttpDnsObj;public static void Init() {AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");if (unityPlayerClass == null) {return;}AndroidJavaObject activityObj = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");if (activityObj == null) {return;}AndroidJavaObject contextObj = activityObj.Call<AndroidJavaObject>("getApplicationContext");// Initialize HTTPDNSAndroidJavaObject httpDnsClass = new AndroidJavaObject("com.tencent.msdk.dns.MSDKDnsResolver");if (httpDnsClass == null) {return;}sHttpDnsObj = httpDnsClass.CallStatic<AndroidJavaObject>("getInstance");if (sHttpDnsObj == null) {return;}AndroidJavaObject dnsConfigBuilder = new AndroidJavaObject("com.tencent.msdk.dns.DnsConfig$Builder");dnsConfigBuilder.Call<AndroidJavaObject>("dnsId", "XXX");dnsConfigBuilder.Call<AndroidJavaObject>("dnsIp", "XXX");dnsConfigBuilder.Call<AndroidJavaObject>("dnsKey", "XXX");// Other configuration informationAndroidJavaObject dnsConfig = dnsConfigBuilder.Call<AndroidJavaObject>("build");sHttpDnsObj.Call("init", contextObj, dnsConfig);// For a version earlier than v4.0.0sHttpDnsObj.Call("init", contextObj, appkey, dnsid, dnskey, dnsIp, debug, timeout);}
getAddrByName
API to resolve a domain. Sample code:// We recommend you perform this operation on a child thread or with Coroutine// Note that you need to call the `AttachCurrentThread` and `DetachCurrentThread` functions before calling the `getAddrByName` API on the child threadpublic static string GetHttpDnsIP(string url) {string ip = string.Empty;AndroidJNI.AttachCurrentThread(); // You need to add this when calling the `getAddrByName` API on the child thread// Get the IP configuration setstring ips = sHttpDnsObj.Call<string>("getAddrByName", url);AndroidJNI.DetachCurrentThread(); // You need to add this when calling the `getAddrByName` API on the child threadif (null != ips) {string[] ipArr = ips.Split(';');if (2 == ipArr.Length && !"0".Equals(ipArr[0]))ip = ipArr[0];}return ip;}
UnityWebRequest www = UnityWebRequest.Get(url);www.certificateHandler = new CertHandler();yield return www.SendWebRequest();// CertHandler is a custom certificate handler class that overrides the ValidateCertificate method to implement custom certificate validation logic.public class CertHandler : CertificateHandler{protected override bool ValidateCertificate(byte[] certificateData){X509Certificate2 certificate = new X509Certificate2(certificateData);X509Certificate2Collection collection = new X509Certificate2Collection();collection.Import(Application.dataPath + "/Certificates/server.cer");return certificate.Issuer == collection[0].Issuer;}}
Was this page helpful?