日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java http2_java的okhttp3库中,客户端如何开启http2协议支持

發布時間:2024/9/19 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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协议支持的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。