Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gzip/Deflate/Bortli not workin okhttp3 4.12.0/5.0.0-alpha12 #8349

Closed
PorITMan opened this issue Apr 7, 2024 · 5 comments
Closed

Gzip/Deflate/Bortli not workin okhttp3 4.12.0/5.0.0-alpha12 #8349

PorITMan opened this issue Apr 7, 2024 · 5 comments
Labels
bug Bug in existing code

Comments

@PorITMan
Copy link

PorITMan commented Apr 7, 2024

etc....
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.brotli.BrotliInterceptor;

private static OkHttpClient.Builder getUnsafeOkHttp() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @SuppressLint("TrustAllX509TrustManager")
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain,
                                                   String authType) {
                    }

                    @SuppressLint("TrustAllX509TrustManager")
                    @Override
                    public void checkServerTrusted(X509Certificate[] chain,
                                                   String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                }
        };

        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new SecureRandom());

        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        return new OkHttpClient.Builder()
                .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])
                .addInterceptor(BrotliInterceptor.INSTANCE)
                .hostnameVerifier((hostname, session) -> true);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Request.Builder request = new Request.Builder();

request.url("https://blablabla.com/");
request.header("accept-encoding", "gzip, deflate, br");

OkHttpClient.Builder http = getUnsafeOkHttp();

http.connectTimeout(3, TimeUnit.SECONDS);
http.addInterceptor(BrotliInterceptor.INSTANCE);

http.build().newCall(request.build()).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull final Call call, @NonNull IOException e) {
                        //
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull final Response response) throws IOException {
                        if (response.code() != 200) {
                            return;
                        }

                        try{
                            String body = response.body().string();

                            JSONObject json = new JSONObject(body);
                        }catch (JSONException e){
                            Log.e(call.request().url().toString());
                            Log.e(response.headers().toString());
                            Log.e(e.toString());
                        }
                    }
                });
https://blablabla.com/

server: AWS
content-type: application/json; charset=utf-8
access-control-allow-credentials: true
access-control-allow-origin: https://blablabla.com
access-control-expose-headers: Content-Disposition
vary: Origin
api-supported-versions: 1.0
strict-transport-security: max-age=15724800; includeSubDomains
access-control-allow-methods: PUT, GET, POST, OPTIONS, DELETE, PATCH
access-control-allow-headers: Authorization, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin, crossdomain, X-SignalR-User-Agent
access-control-max-age: 1728000
date: Sun, 07 Apr 2024 21:18:49 GMT
content-encoding: gzip
content-length: 252

org.json.JSONException: Value ���??????????����1N�0�E�H�C���y��� of type java.lang.String cannot be converted to JSONObject
https://blablabla.com/

server: AWS
content-type: application/json; charset=utf-8
access-control-allow-credentials: true
access-control-allow-origin: https://blablabla.com
access-control-expose-headers: Content-Disposition
vary: Origin
api-supported-versions: 1.0
strict-transport-security: max-age=15724800; includeSubDomains
access-control-allow-methods: PUT, GET, POST, OPTIONS, DELETE, PATCH
access-control-allow-headers: Authorization, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin, crossdomain, X-SignalR-User-Agent
access-control-max-age: 1728000
date: Sun, 07 Apr 2024 21:22:29 GMT
content-encoding: br
content-length: 210

org.json.JSONException: Value �t�??��O���f������cz@ of type java.lang.String cannot be converted to JSONObject

@PorITMan PorITMan added the bug Bug in existing code label Apr 7, 2024
@yschimke
Copy link
Collaborator

yschimke commented Apr 7, 2024

It should work anyway, but you are applying it twice. Try only once.

@PorITMan
Copy link
Author

PorITMan commented Apr 7, 2024

It should work anyway, but you are applying it twice. Try only once.

I did it once, then twice. Gzip transparently doesn't work either

@yschimke
Copy link
Collaborator

yschimke commented Apr 8, 2024

You don't need to set the accept-encoding header yourself. That disables the automatic compression.

@yschimke yschimke closed this as not planned Won't fix, can't repro, duplicate, stale Apr 8, 2024
@PorITMan
Copy link
Author

PorITMan commented Apr 8, 2024

You don't need to set the accept-encoding header yourself. That disables the automatic compression.

Yes it worked. And I need the line to be: accept-encoding: gzip, deflate, br

if you do not add accept-encoding, then the line accept-encoding: gzip, br

if: http.protocols(List.of(Protocol.H2_PRIOR_KNOWLEDGE));

Automatic unpacking is also disabled.

@yschimke
Copy link
Collaborator

yschimke commented Apr 8, 2024

If you need explicit control, you will need to take over decompression also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

2 participants