簡體   English   中英

OkHttp“java.net.ProtocolException:意外狀態行:nullHTTP / 1.1 200 OK”在使用相同連接的“204 NO-CONTENT”響應之后

[英]OkHttp “java.net.ProtocolException: Unexpected status line: nullHTTP/1.1 200 OK” after a “204 NO-CONTENT” response using same connection

我需要使用相同的連接執行一系列調用(okhttpclient符合我的需要),其中一個返回204狀態代碼(無正文),但在“204調用”后成功:

java.net.ProtocolException: Unexpected status line: nullHTTP/1.1 200 OK
at okhttp3.internal.http.StatusLine.parse(StatusLine.java:69)
at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
at okhttp3.RealCall.getResponse(RealCall.java:244)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.execute(RealCall.java:57)
at it.test.Main.main(Main.java:48)

使用請求標頭“連接:關閉”(因此不重用連接)問題不會發生。

這是一個報告問題的工作主要人員。 它使用okHttp v3.3.0和okio v1.8.0

public static void main(String[] args) {
try {
    String url200 = "http://www.cmsservice.provahosting.it/index.php?option=com_cmsmanager&key=ca25131a2f77fb2b80d82e63412fdc13&debug=1&cmd=status";
    String url204 = "http://www.cmsservice.provahosting.it/index.php?option=com_cmsmanager&key=ca25131a2f77fb2b80d82e63412fdc13&debug=1&cmd=getUpdates";

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS).build();

    //First request
    Request request = new Request.Builder()
            .url(url200)/*.addHeader("Connection", "close")*/
            .build();

    Response response = client.newCall(request).execute();
    System.out.println(response.code()+">>>>" + response.body().string().getBytes().length + "<<<<");
    response.body().close();

    //Second request
    Request request2 = new Request.Builder()
            .url(url204)/*.addHeader("Connection", "close")*/
            .build();
    response = client.newCall(request2).execute();
    System.out.println(response.code()+">>>>" + response.body().string().getBytes().length + "<<<<");
    response.body().close();

    //Third request
    Request request3 = new Request.Builder()
            .url(url200)/*.addHeader("Connection", "close")*/
            .build();
    response = client.newCall(request3).execute();
    System.out.println(response.code()+">>>>" + response.body().string().getBytes().length + "<<<<");
    response.body().close();

} catch (Exception e) {
    e.printStackTrace();
}

有人可以幫助我理解我錯過的東西或者它是否是一個錯誤?

看起來你的網絡服務器聲稱先前的響應沒有正文,但隨后寫了一個四字符的主體“null”。 將問題報告給網絡服務器的維護人員,他們應該修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM