百度智能云

All Product Document

          Object Storage

          Use HTTPDNS Service Through Android SDK

          Scenario Overview

          Based on the use of BOS, the current mobile upload problems are mainly concentrated in the following aspects:

          • DNS resolution failed, request error;
          • Domain name hijacking, data tampering during transmission, which brings users network access risks such as fishing and privacy theft;
          • Under a weak network, the speed is slow, and data upload may time out.

          Access to BOS via HTTPDNS service has the following advantages:

          • Secure and anti-hijacking, able to effectively reduce the success rate drop caused by domain name hijacking;
          • Precise scheduling can provide optimal access points and reduce user access delay;
          • When the domain name resolution result is changed, the HTTPDNS service is not affected by the multi-level cache of the traditional DNS service, but able to make the mobile terminal get the new resolution result faster, avoid the impact of multi-level cache, and effectively shorten the effective time of domain name switching.

          Applicable Scenario: Scenarios that are sensitive to request success rate, delay, and failure stop loss efficiency when using BOS.

          Operating Procedures

          Enable HTTPDNS service

          • Enter HTTPDNS official document to enable the service;
          • Add *.bcebos.com in the domain name management of [Configuration Management], or the custom domain name or CDN domain name used by the business;
          • Test HTTPDNS.

          Add project dependencies

          We provide a convenient remote dependency for users to use httpdns.

          • Open the build.gradle of the module that needs to reference the SDK, and add at the end:
          repositories{ 
              jcenter() 
              maven { 
                  url "https://raw.githubusercontent.com/bdhttpdns/BDHttpDnsSDK/master" 
              } 
          } 
          • Add under dependencies:
          dependencies{ 
              compile 'com.baidu:httpdns:1.3' 
          } 

          Reload OkHttpDns

          Set the account and password of the HTTPDNS service just opened, and preload common domain names to avoid cache misses during the first request. At the same time, you can also set a variety of processing strategies to better fulfill business needs. Refer to mobile domain name resolution Android_SDK setting interface.

          static class OkHttpDns implements Dns { 
              private static OkHttpDns instance = null; 
              private BDHttpDns httpDns; 
              
              private OkHttpDns(Context context) { 
                  this.httpDns = BDHttpDns.getService(context); 
                  this.httpDns.setAccountID(account); 
                  this.httpDns.setSecret(secret); 
                  // Preload domain name 
                  ArrayList<String> preResolveHosts = new ArrayList<String>(); 
                  preResolveHosts.add("bj.bcebos.com"); 
                  this.httpDns.setPreResolveHosts(preResolveHosts); 
              } 
              
              public static OkHttpDns getInstance(Context context) { 
                  if(instance == null) { 
                      instance = new OkHttpDns(context); 
                  } 
                  return instance; 
              } 
              
              @Override 
              public List<InetAddress> lookup(final String hostname) throws UnknownHostException { 
                  // Get ip through synchronous resolving interface 
                  final BDHttpDnsResult httpDnsResult = httpDns.syncResolve(hostname, false); 
                  Log.v("dns", "HttpDns resolve type: " + httpDnsResult.getResolveType()); 
                  // Use ipv6 results first; for ipv6 results, add [] characters before and after ip 
                  // If no ipv6 result is available, use ipv4 result 
                  ArrayList<String> ipv6List = httpDnsResult.getIpv6List(); 
                  ArrayList<String> ipv4List = httpDnsResult.getIpv4List(); 
                  String ip = null; 
                  if (ipv6List != null && !ipv6List.isEmpty()) { 
                      ip = "[" + ipv6List.get(0) + "]"; 
                  } else if (ipv4List != null && !ipv4List.isEmpty()) { 
                      ip = ipv4List.get(0); 
                  } else { 
                      Log.v("DNS", "Get empty iplist from httpdns, use origin url"); 
                  } 
                  if(ip != null) { 
                      final String finalIp = ip; 
                      // If ip is not null, directly use the ip to make network requests 
                      List<InetAddress> inetAddresses = Arrays.asList(InetAddress.getAllByName(ip)); 
                      Log.d("DNS", "inetAddresses:" + inetAddresses); 
                      return inetAddresses; 
                  } 
                  // If it returns null, use the system DNS service to resolve the domain name 
                  return Dns.SYSTEM.lookup(hostname); 
              } 
          } 

          Adjust Bosclient configuration

          Use setDns.

          BosClientConfiguration config = new BosClientConfiguration(); 
          config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKey)); 
          config.setEndpoint(bos_endpoint); 
          config.setProtocol(Protocol.HTTP); 
          config.setDns(OkHttpDns.getInstance(getApplicationContext())); 

          Experience now

          After the above process is completed, you can experience the HTTPDNS service by BOS normally.

          Note

          • Although HTTPDNS has a certain free quota (currently there are 3 million free domain name resolutions per natural month), a certain cost will be incurred after the initial quota is exceeded. Through the dns metric configuration, you can reduce the access to the httpdns server, for example: Cache expiration processing strategy, using https protocol or http protocol, etc.
          • When the APP is just started, the default HTTPDNS downgrade flag is Yes. The SDK and network library automatically use DNS resolution to initiate requests. This is normal and will not affect the subsequent use of HTTPDNS service to access BOS.
          Previous
          Upload Data to BOSCDN by CDN Dynamic Acceleration
          Next
          S3