
dnsId parameter in the SDK used for DNS authentication.dnsKey parameter in the SDK, which you need to pass in when using the DES encryption method.dnsKey parameter in the SDK, which you need to pass in when using the AES encryption method.appkey of the SDK for Android used for authentication.HttpDNSLibs\\HTTPDNS_ANDROID_xxxx.aar to the corresponding location in the application's libs folder.build.gradle in the app module:android {// ...repositories {flatDir {dirs 'libs'}}}dependencies {// ...implementation(name: 'HTTPDNS_Android_xxxx', ext: 'aar')// In v4.3.0, local data storage is added. You also need to add a dependency on Room.implementation "androidx.room:room-rxjava2:2.2.0"}
<dependency><groupId>io.github.dnspod</groupId><artifactId>httpdns-sdk</artifactId><version>4.4.0</version><type>aar</type></dependency><!-- SDK for Tencent Cloud International --><dependency><groupId>io.github.dnspod</groupId><artifactId>httpdns-sdk</artifactId><version>4.4.0-intl</version><type>aar</type></dependency>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.INTERNET" /><!-- Optional and used to get the mobile phone's IMEI for data reporting --><uses-permission android:name="android.permission.READ_PHONE_STATE" />
AndroidManifest file.<?xml version="1.0" encoding="utf-8"?><manifest ... ><application android:networkSecurityConfig="@xml/network_security_config"... >...</application></manifest>
network_security_config.xml configuration file in the XML directory.<?xml version="1.0" encoding="utf-8"?><network-security-config><base-config cleartextTrafficPermitted="true" /></network-security-config>
setUseExpiredIpEnable(true) and setCachedIpEnable(true) to achieve optimistic DNS cache.0;0 is returned, and an async DNS request is initiated to update the cache. Therefore, after enabling optimistic DNS cache, you need to ensure that local DNS will work if no cache is hit. For important domains, we recommend you enable DNS prefetch via preLookupDomains(String... domainList).persistentCacheDomains(String... domainList) and DNS prefetch via preLookupDomains(String... domainList) to ensure the accuracy of DNS results.DnsConfig dnsConfigBuilder = DnsConfig.Builder()// (Required) DNS ID, i.e., authorization ID, which can be applied for and obtained in the HTTPDNS console (https://console.tencentcloud.com/httpdns) for DNS authentication.dnsId("xxx")// (Required) DNS key, i.e., the key corresponding to the authorization ID, which can be obtained in Development Configuration (https://console.tencentcloud.com/httpdns/configure) for DNS authentication.dnsKey("xxx")// [Disused from v4.5.0] Internally scheduled by the SDK// (Required) Use `43.132.43.43` (default value) if `channel` is `desHttp()` or `aesHttp()`..dnsIp("xxx")// (Optional) Channel configuration: The default value is `desHttp()` (DES encryption based on HTTP request). Valid values also include `aesHttp()`..desHttp()// (Optional) Log granularity. If DEBUG printing is enabled, pass in "Log.VERBOSE".logLevel(Log.VERBOSE)// (Optional) Domains for DNS prefetch, Please enter the full domain name in the format of `"www.baidu.com", "www.qq.com"`. It is recommended not to set too many domain names. You can configure up to 8 domains. DNS prefetch is triggered only during initialization..preLookupDomains("www.baidu.com", "www.qq.com")// (Optional) Automatically refresh DNS cache. Please enter the full domain name in the format of `"www.baidu.com", "www.qq.com"`. For the configured domains, the SDK will initiate DNS requests automatically to update their cache when 75% of the TTL has elapsed, so that cache will always be hit for these domains. It is recommended not to set too many domain names. You can configure up to 8 domains. Configure the domains for automatic cache update and those for DNS prefetch separately..persistentCacheDomains("www.baidu.com", "www.qq.com")// (Optional) Items for IP address ranking, configured as a list of `IpRankItem(hostname, port)`, where port is optional and defaults to 8080. An example value is `IpRankItem("qq.com", 443)`. The SDK will perform a socket connection speed test for the configured items and rank the resolved IP addresses. IP address ranking does not block the current DNS query and takes effect at the next query. You can configure up to 10 items..ipRankItems(ipRankItemList)// (Optional) Manually specify the support of network stack. Pass in 1 for IPv4 resolution only, pass in 2 for IPv6 resolution only, and pass in 3 for IPv4/IPv6 dual stack resolution. By default, a DNS request will be initiated according to the support of the client's local network stack.setCustomNetStack(3)// (Optional) Set whether to allow the use of expired cache. The default value is `false`. When the value is `false`, the SDK preferentially obtains the unexpired cache result. If this cannot be obtained, it will wait for the DNS request to complete and then return the result.// (Optional) Set whether to allow the use of expired cache. When this parameter is set to `true`, the SDK directly returns the cached DNS result or `0;0` if there is no cached result, and initiates an async DNS request to update the cache if there is no cached result or the cache has expired. Since the async APIs (getAddrByNameAsync and getAddrsByNameAsync) always return the unexpired DNS result in the callback, the async APIs are unavailable when this parameter is set to `true`. In this case, we recommend you use the sync APIs (getAddrByName and getAddrsByName). You can use `localdns (InetAddress)` to return the result if HTTPDNS fails to return a result..setUseExpiredIpEnable(false)// (Optional) Set whether to enable the local cache (Room). The default value is `false`..setCachedIpEnable(false)// (Optional) Set the DNS request timeout period, which is 2000 ms by default.timeoutMills(2000)// (Optional) The ECS (EDNS-Client-Subnet) value of the DNS request. By default, the HTTPDNS server will query the client's egress IP in order to query the IP for the DNS split zone. You can specify the split zone's IP address by passing in an IPv4/IPv6 address.routeIp("XXX")// (Optional) [Disused from v4.4.0] The SDK log reporting capability is configured in the console..enableReport(true)// End with `build()`.build();MSDKDnsResolver.getInstance().init(this, dnsConfigBuilder);
43.132.43.43 .43.132.43.43/43.132.55.55.HttpDnsSample directory).val host = System.getProperty("http.proxyHost")val port = System.getProperty("http.proxyPort")if (null != host && null != port) {// An HTTP proxy is used locally}
true for the debug parameter in the init API to filter logs with the HTTPDNS tag, if you see logs related to the local DNS (ldns_ip) and HTTPDNS (hdns_ip), the connection is correct.ldns_ip key indicates the DNS query results provided by the local DNS.hdns_ip key indicates the DNS query results of A records provided by HTTPDNS.hdns_4a_ips key indicates the DNS query results of AAAA records provided by HTTPDNS.a_ips key indicates the set of IPv4 addresses returned by the DNS API.4a_ips key indicates the set of IPv6 addresses returned by the DNS API.tcpdump command to capture packets.
xw.qq.com is eventually sent to the server at the IP address 183.3.226.35.
xw.qq.com is eventually sent to the server at the IP address 183.3.226.35.getAddrByName is a time-consuming sync API, so it should be called on a child thread.URLConnection as an example:URL oldUrl = new URL(url);URLConnection connection = oldUrl.openConnection();// Get the HTTPDNS query resultString ips = MSDKDnsResolver.getInstance().getAddrByName(oldUrl.getHost());String[] ipArr = ips.split(";");if (2 == ipArr.length && !"0".equals(ipArr[0])) { // Get the IP successfully through HTTPDNS, replace the URL, and set the host headerString ip = ipArr[0];String newUrl = url.replaceFirst(oldUrl.getHost(), ip);connection = (HttpURLConnection) new URL(newUrl).openConnection(); // Set the host field of the HTTP request headerconnection.setRequestProperty("Host", oldUrl.getHost());}
www.qq.com, and the IP obtained by HTTPDNS is 192.168.0.111, then you can access it as follows:curl -H "Host:www.qq.com" http://192.168.0.111/aaa.txt
String host = System.getProperty("http.proxyHost");String port= System.getProperty("http.proxyPort");if (null != host && null != port) {// Local proxy mode is used}
Feedback