tencent cloud

All product documents
Last updated: 2025-04-07 16:53:29
OkHttp
Last updated: 2025-04-07 16:53:29

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/WebSocket + SNI), which causes little intrusion. Below is a sample:
OkHttpClient mOkHttpClient =
new OkHttpClient.Builder()
.dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) {
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();

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

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
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon