java http2_java的okhttp3库中,客户端如何开启http2协议支持
題目描述
我在實現向蘋果的APNs進行推送的工作,因為APNs需要http2協議,所以我在代碼中使用了okhttp3這個庫來提供客戶端的http2支持。okhttp3是3.14.0版本。
但當我使用
response = client.newCall(request).execute()
這個同步方法向APNs發送POST數據時,遇到了 java.io.IOException: unexpected end of stream on https://api.development.push....
的異常。
題目來源及自己的思路
我根據在網上搜索的一些方法嘗試了不同的參數或配置,但是都沒有效果。
經過分析異常初步覺得是APNs返回http2數據,但是客戶端默認使用的還是HTTP1.1,導致庫讀取reponse時用的還是HTTP1.1的相關類和方法,按HTTP1.1的格式去讀取HeaderLine而引發了異常。
我嘗試的一些配置代碼如下:
1.配置H2協議
OkHttpClient.Builder builder = new OkHttpClient.Builder();
List protocols = new ArrayList<>();
// protocols.add(Protocol.H2_PRIOR_KNOWLEDGE);
protocols.add(Protocol.HTTP_2);
protocols.add(Protocol.HTTP_1_1);
builder.protocols(protocols);
2.開啟連接重試
builder.retryOnConnectionFailure(true);
3.配置協議升級的頭字段
Request.Builder rb = new Request.Builder();
rb.addHeader("Connection", "Upgrade, HTTP2-Settings").addHeader("Upgrade", "h2c");
4.減小連接keepalive時間
builder.connectionPool(new ConnectionPool(10, 30, TimeUnit.SECONDS));
相關代碼
// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)
下面是異常信息:
java.io.IOException: unexpected end of stream on https://api.development.push.apple.com/...
NotificationResponse{error=null, httpStatusCode=-1, responseBody='null', cause=java.io.IOException: unexpected end of stream on https://api.development.push.apple.com/...}
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:233)
at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
at okhttp3.RealCall.execute(RealCall.java:81)
at com.clevertap.apns.clients.SyncOkHttpApnsClient.push(SyncOkHttpApnsClient.java:338)
at com.clevertap.apns.ClientDemo.req_apns(ClientDemo.java:164)
at com.clevertap.apns.ClientDemo.main(ClientDemo.java:185)
Caused by: java.io.EOFException: \n not found: limit=159 content=0000180400000000000001000010000003000003e8000500004000000600001f…
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:240)
at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:238)
at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213)
... 19 more
你期待的結果是什么?實際看到的錯誤信息又是什么?
請問OKHTTP中如何開啟對HTTP2的支持?或者請問我這個異常的真實原因是什么,又如何解決呢?
請不吝指點,非常感謝。
總結
以上是生活随笔為你收集整理的java http2_java的okhttp3库中,客户端如何开启http2协议支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 胡润独角兽是什么意思
- 下一篇: 在java中arraylist_在Jav