tencent cloud

HTTPDNS

Product Introduction
Overview
Scenarios
Strengths
Use Limits
Purchase Guide
Billing Overview
Payment Overdue
Getting Started
Activating HTTPDNS
Connecting to HTTPDNS
Operation Guide
Adding a Domain
DNS Statistics Description
Query Monitoring
API Documentation
Configuration Information Description
Querying with HTTP Request Methods
AES/DES Encryption/Decryption
Practical Tutorial of API Connection
SDK Documentation
SDK Quick Access
SDK for iOS
SDK for Android
Access Management
Overview
Sample Access Control Policy
FAQs
HTTPDNS Policy
Privacy Policy
Data Processing And Security Agreement

Unity

PDF
Focus Mode
Font Size
Last updated: 2023-06-12 14:45:51
Prepare for the integration.
Obtain the SDK file and copy it to the Assets/Plugins/Android directory of the Unity project.
Initialize HTTPDNS. Sample code:
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 HTTPDNS
AndroidJavaObject httpDnsClass = new AndroidJavaObject("com.tencent.msdk.dns.MSDKDnsResolver");
if (httpDnsClass == null) {
return;
}
sHttpDnsObj = httpDnsClass.CallStatic<AndroidJavaObject>("getInstance");
if (sHttpDnsObj == null) {
return;
}
// For v4.0.0 or later (recommended). For more information, see Android SDK
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 information
AndroidJavaObject dnsConfig = dnsConfigBuilder.Call<AndroidJavaObject>("build");
sHttpDnsObj.Call("init", contextObj, dnsConfig);
// For a version earlier than v4.0.0
sHttpDnsObj.Call("init", contextObj, appkey, dnsid, dnskey, dnsIp, debug, timeout);
}
Call the 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 thread
public 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 set
string ips = sHttpDnsObj.Call<string>("getAddrByName", url);
AndroidJNI.DetachCurrentThread(); // You need to add this when calling the `getAddrByName` API on the child thread
if (null != ips) {
string[] ipArr = ips.Split(';');
if (2 == ipArr.Length && !"0".Equals(ipArr[0]))
ip = ipArr[0];
}
return ip;
}
For the information about HTTPS certificate validation in a direct IP connection scenario, see here. Sample code:
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;
}
}


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback