OkHttp“java.net.ProtocolException:意外状态行:nullHTTP / 1.1 200 OK”在使用相同连接的“204 NO-CONTENT”响应之后

时间:2016-05-25 09:57:06

标签: java http okhttp okhttp3

我需要使用相同的连接执行一系列调用(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();
}

有人可以帮我理解我错过的内容或者是否是一个错误? TNX

1 个答案:

答案 0 :(得分:2)

看起来您的网络服务器声称先前的响应没有正文,但随后写了一个四字符正文“null”。将问题报告给网络服务器的维护人员,他们应该修复它。

相关问题