tencent cloud

Feedback

Last updated: 2023-06-12 14:47:38

    OkHttp

    OkHttp provides a DNS API to inject a DNS implementation into OkHttp. Thanks to the excellent design of OkHttp, when OkHttp is used to access the network, you can connect to HTTPDNS to resolve domains by implementing the DNS API with no extra work required even in complex scenarios (HTTP/HTTPS/WebSocket + SNI), which causes little intrusion. Below is a sample:
    mOkHttpClient =
    new OkHttpClient.Builder()
    .dns(new Dns() {
    @NonNull
    @Override
    public List<InetAddress> lookup(String hostname) {
    Utils.checkNotNull(hostname, "hostname can not be null");
    String ips = MSDKDnsResolver.getInstance().getAddrByName(hostname);
    String[] ipArr = ips.split(";");
    if (0 == ipArr.length) {
    return Collections.emptyList();
    }
    List<InetAddress> inetAddressList = new ArrayList<>(ipArr.length);
    for (String ip : ipArr) {
    if ("0".equals(ip)) {
    continue;
    }
    try {
    InetAddress inetAddress = InetAddress.getByName(ip);
    inetAddressList.add(inetAddress);
    } catch (UnknownHostException ignored) {
    }
    }
    return inetAddressList;
    }
    })
    .build();
    Note
    Implementing the DNS API means that all network requests processed by the current OkHttpClient instance will go through HTTPDNS. If you have only a small number of domains to be resolved by HTTPDNS, we recommend you filter them before calling the DNS API of HTTPDNS.

    Retrofit + OkHttp

    Retrofit is actually a library that adds a layer of encapsulation bridging to the API based on OkHttp. Therefore, you can connect to HTTPDNS simply by customizing the OkHttpClient in Retrofit like in the OkHttp connection method as shown below:
    mRetrofit =
    new Retrofit.Builder()
    .client(mOkHttpClient)
    .baseUrl(baseUrl)
    .build();
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support