Very slow connection with OkHttp ipv6 enabled domain on Android Studio

327 Views Asked by At

I have two domain pointing to the same server, one have ipv6 enabled, the other one doesn't.

4 scenario for sending request

  1. ipv6 enabled, using wifi, return in about 4sec
  2. ipv6 enabled, using cellular data, return in about 36SEC
  3. ipv6 disabled, using wifi, return in about 4sec
  4. ipv6 disabled, using cellular data, return in about 6sec

Below is my java code, hopefully someone can help me with the ipv6 enabled, using cellular data situation..

    protected int executePost(String _url, byte[] _data) {
        OkHttpClient bootstrapClient = new OkHttpClient.Builder()
                .callTimeout(20, TimeUnit.SECONDS)
                .cache(new Cache(new File("/local/cacheDirectory"), 10 * 1024 * 1024))
                .retryOnConnectionFailure(false)
                .followRedirects(false)
                .followSslRedirects(false)
                .build();

        Dns dns = new DnsOverHttps.Builder().client(bootstrapClient)
                .url(HttpUrl.get("https://cloudflare-dns.com/dns-query"))
                .bootstrapDnsHosts(getByIp("1.1.1.1"), getByIp("1.0.0.1"))
                .includeIPv6(true)
                .post(true)
                .build();

        OkHttpClient client = bootstrapClient.newBuilder().dns(dns).build();

        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody postBody = RequestBody.create(_data, JSON);
        Request post = new Request.Builder()
                .url(_url)
                .post(postBody)
                .addHeader("Content-Type", "application/json")
                .build();

        try (Response response = client.newCall(post).execute()) {
            m_httpStatus = response.code();
            m_httpBody = response.body().string();
        } catch(Exception e) {
            m_httpStatus = -1;
            m_httpError = e.getCause().toString();
        }

        return m_httpStatus;
    }

p.s. I'm using okhttp:5.0.0-alpha.11, and I am sure my device have ipv6.

1

There are 1 best solutions below

0
On

The problem lies in this line of code

bootstrapDnsHosts(getByIp("1.1.1.1"), getByIp("1.0.0.1"))

when client is using ipv6, it couldn't access doh via ipv4 address, all you have to do is to determine if client is using ipv4 or ipv6, and then use associated address.