1

我正在为使用身份验证令牌的推送通知编写 JUNIT。现在,Apple 推送 API 基于 HTTP2,我想为此目的使用 spring WebClient。

private static WebClient webClient = WebClient
            .builder()
            .baseUrl("https://api.development.push.apple.com/3/device")
                .defaultHeader("apns-priority", "10")
                .defaultHeader("apns-expiration", "0")
                .build();

    class ApnsResponse {
        String reason;
        String timestamp;
    }

    @Test
    void test1() {
        Hooks.onOperatorDebug();
        String message = "{\"aps\":{\"alert\":\"hello\"}}";
        try {
        webClient
            .post()
                .uri("/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .header("authorization", "bearer "+authToken)
                .header("apns-topic", "com.demo.12316")
                .bodyValue(message)
                .retrieve()
                .bodyToMono(ApnsResponse.class)
                .block();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

当我运行测试用例时,我得到:

21:37:26.028 [reactor-http-nio-1] DEBUG io.netty.handler.ssl.SslHandler - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] HANDSHAKEN: protocol:TLSv1.2 cipher suite:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
21:37:26.030 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}, [connected])
21:37:26.062 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(GET{uri=/, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [configured])
21:37:26.070 [reactor-http-nio-1] DEBUG reactor.netty.http.client.HttpClientConnect - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] Handler is being applied: {uri=https://api.development.push.apple.com/3/device/6d268d2d494f512dfa1212aa612c1822f44df4375e9116b5a52109f3a21d1, method=POST}
21:37:26.075 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [request_prepared])
21:37:26.111 [reactor-http-nio-1] DEBUG org.springframework.core.codec.CharSequenceEncoder - [6304101a] Writing "{"aps":{"alert":"hello"}}"
21:37:26.183 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [request_sent])
21:37:26.559 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] Channel closed, now 0 active connections and 0 inactive connections
21:37:26.569 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443]}}, [response_incomplete])
21:37:26.592 [reactor-http-nio-1] WARN reactor.netty.http.client.HttpClientConnect - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] The connection observed an error
reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
reactor.core.Exceptions$ReactiveException: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

我能够使用 WebClient 进行 HTTP 调用,但在这种情况下它只是不起作用。我正在使用 JAVA 1.8。我什至尝试在引导类路径中添加 ALPN,但仍然出现同样的错误。

WebClient 是否支持 HTTP2?或者我必须开始研究 OkHTTP2。

同时,我正在阅读有关在创建 WebClient 时使用的 clientConnector

4

0 回答 0