Skip to content

REFUSED_STREAM with Okhttp and Nginx #7642

@rahuljiresal

Description

@rahuljiresal

Okhttp fails when there is REFUSED_STREAM in HTTP/2. This happens with newer versions of nginx that use HTTP/2. GET requests get processed correctly, however, for POST, it fails with nginx complaining that the client prematurely closed the stream.

client sent stream with data before settings were acknowledged while processing HTTP/2 connection

More info here - square/okhttp#2543 and square/okhttp#2506

Because of this, okhttp does not work with some of the newer nginx servers.

The fix is to configure OkHttpClient with other protocols for it to use. Here's the fix -- wallabag/android-app#256.

As per OkHttp's javadocs,

The client will prefer the most efficient transport available, falling back to more ubiquitous protocols.

If multiple protocols are specified, ALPN will be used to negotiate a transport.

However, if you enable all possible protocols (like I tried in the PR here #7641), you will still get PROTOCOL_ERRORS maybe half the time. So instead of the change, the solution might be to add this to your MainActivity.

    @Override
    protected void onStart() {
        super.onStart();

        OkHttpClient client = OkHttpClientProvider.getOkHttpClient();
        List<Protocol> protocolList = new ArrayList<>();
        protocolList.add(Protocol.HTTP_1_1);
        client.setProtocols(protocolList);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions