Using HTTPDNS Service via Android SDK
Overview
This article explains how to use the HTTPDNS service to access BOS.
Requirement scenarios
-
Based on the usage of BOS, the current issues with mobile uploads mainly focus on the following aspects:
(1) DNS resolution failure, resulting in request errors;
(2) Domain name hijacking, where data is tampered with during transmission, bringing network access risks to users such as phishing and privacy theft;
(3) Under poor network conditions, the speed can be slow, and data uploads might time out.
-
If you use the HTTPDNS service to access BOS, you will have the following advantages:
(1) Security against hijacking, which can effectively reduce the problem of reduced success rates caused by domain name hijacking;
(2) Precise scheduling can provide the optimal access point and reduce user access delay;
(3) When domain name resolution results change, the HTTPDNS service is not affected by the multi-tier caching of traditional DNS services. This allows mobile devices to acquire new resolution results more quickly, bypassing multi-layer caching and effectively reducing the latency in domain name switching.
Therefore, if you are sensitive to the request success rate, delay, and fault stop-loss speed of BOS, you can use the HTTPDNS service to access BOS.
Solution overview
To access BOS using the HTTPDNS service, you need to activate the HTTPDNS service, add project dependencies, reload OkHttpDns, and adjust the Bosclient configuration.
Notes:
- Although HTTPDNS includes a certain free quota (currently 3 million free domain name resolutions per natural month), exceeding this quota will incur additional fees. You can decrease the frequency of HTTPDNS server access by configuring parameters such as cache expiration strategies and selecting either HTTPS or HTTP protocols.
- When the app is launched for the first time, the default HTTPDNS downgrade option is enabled. The SDK and network library automatically use DNS resolution to send requests. This is a standard process and does not affect the subsequent usage of the HTTPDNS service for accessing BOS.
Practical operations
1. Activate the HTTPDNS service
- Go to the HTTPDNS Official Documentation and complete the service activation;
- In the domain name management under Configuration Management, add
*.bcebos.com, or the custom domain name or CDN domain name used by the business; - Test the functionality of HTTPDNS.
2. Add project dependencies
We provide an easy-to-use remote dependency setup for utilizing HTTPDNS.
- Open the build.gradle of the module that needs to reference this SDK and add the following at the end:
1repositories{
2 jcenter()
3 maven {
4 url "https://raw.githubusercontent.com/bdhttpdns/BDHttpDnsSDK/master"
5 }
6}
- Add under dependencies:
1dependencies{
2 compile 'com.baidu:httpdns:1.3'
3}
3. Reload OkHttpDns
Set the account and password for the newly activated HTTPDNS service, and preload commonly used domain names to avoid cache misses during the initial request. At the same time, you can also set various processing strategies to better meet business needs. For reference, see Mobile Domain Name Resolution Android_SDK Setting Interface.
1static class OkHttpDns implements Dns {
2 private static OkHttpDns instance = null;
3 private BDHttpDns httpDns;
4
5 private OkHttpDns(Context context) {
6 this.httpDns = BDHttpDns.getService(context);
7 this.httpDns.setAccountID(account);
8 this.httpDns.setSecret(secret);
9 // Preload domain names
10 ArrayList<String> preResolveHosts = new ArrayList<String>();
11 preResolveHosts.add("bj.bcebos.com");
12 this.httpDns.setPreResolveHosts(preResolveHosts);
13 }
14
15 public static OkHttpDns getInstance(Context context) {
16 if(instance == null) {
17 instance = new OkHttpDns(context);
18 }
19 return instance;
20 }
21
22 @Override
23 public List<InetAddress> lookup(final String hostname) throws UnknownHostException {
24 // Obtain IP through synchronous resolution interface
25 final BDHttpDnsResult httpDnsResult = httpDns.syncResolve(hostname, false);
26 Log.v("dns", "HttpDns resolve type: " + httpDnsResult.getResolveType());
27 // Give priority to using IPv6 results. For IPv6 results, you need to add [] characters before and after the IP
28 // If there is no IPv6 result, use the IPv4 result
29 ArrayList<String> ipv6List = httpDnsResult.getIpv6List();
30 ArrayList<String> ipv4List = httpDnsResult.getIpv4List();
31 String ip = null;
32 if (ipv6List != null && !ipv6List.isEmpty()) {
33 ip = "[" + ipv6List.get(0) + "]";
34 } else if (ipv4List != null && !ipv4List.isEmpty()) {
35 ip = ipv4List.get(0);
36 } else {
37 Log.v("DNS", "Get empty iplist from httpdns, use origin url");
38 }
39 if(ip != null) {
40 final String finalIp = ip;
41 // If the IP is not null, use the IP directly for network requests
42 List<InetAddress> inetAddresses = Arrays.asList(InetAddress.getAllByName(ip));
43 Log.d("DNS", "inetAddresses:" + inetAddresses);
44 return inetAddresses;
45 }
46 // If null is returned, use the system DNS service to resolve the domain name
47 return Dns.SYSTEM.lookup(hostname);
48 }
49}
4. Adjust Bosclient configuration
Utilize the setDns method.
1BosClientConfiguration config = new BosClientConfiguration();
2config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKey));
3config.setEndpoint(bos_endpoint);
4config.setProtocol(Protocol.HTTP);
5config.setDns(OkHttpDns.getInstance(getApplicationContext()));
5. Experience immediately
After completing the steps above, you can seamlessly experience HTTPDNS domain name resolution services while using BOS.
