Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java okHttp重用保持活动连接_Java_Session_Keep Alive_Okhttp_Session Reuse - Fatal编程技术网

Java okHttp重用保持活动连接

Java okHttp重用保持活动连接,java,session,keep-alive,okhttp,session-reuse,Java,Session,Keep Alive,Okhttp,Session Reuse,如何通过okHttp重用http保持活动连接 我的代码示例: public class MainWithOkHttp { static OkHttpClient _client = new OkHttpClient(); public static void main(String[] args) { ... query1(); ... // in this request Request _re

如何通过okHttp重用http保持活动连接

我的代码示例:

public class MainWithOkHttp {

    static OkHttpClient _client = new OkHttpClient();

    public static void main(String[] args) {
        ...
        query1();
        ...
        // in this request
        Request _request = new Request.Builder()
            .url("...")
            .addHeader("connection", "keep-alive")
            .addHeader("cookie", _cookie)
            .post(postParams)
            .build();
        // in this request my previous session is closed, why?
        // my previous session = session created in query1 method
        Response _response = _client.newCall(_request).execute();
        ...
    }

    ...
    private static void query1() {
        ...
        Request request = new Request.Builder()
            .url("...")
            .addHeader("connection", "keep-alive")
            .addHeader("cookie", _cookie)
            .get()
            .build();
        Response _response = _client.newCall(request).execute();
        ...
    }
    ...

}
因此,我正在调用
query1()
方法。在这个方法中,连接被打开,服务器端的会话被创建,并且带有sessionId的cookie被接收

但是,当我对服务器执行另一个查询时,我以前的连接未被使用,服务器上的会话已关闭。服务器中的会话生存期不小,因此问题不在生存期内


PS:我从服务器获取验证码并识别验证码,然后使用验证码对服务器执行查询。但服务器端的验证码无法识别,因为会话已关闭,验证码存储在会话中。

最终,以下代码对我有效:

public class Main {

  static OkHttpClient _client = new OkHttpClient();

  public static void main(String[] args) throws IOException {
      performQuery();
  }

  private static void performQuery() throws IOException {
      Request firstRequest = new Request.Builder()
            .url("http://localhost/test/index.php")
            .get()
            .build();
      Response firstResponse = _client.newCall(firstRequest).execute();

      Request.Builder requestBuilder = new Request.Builder()
            .url("http://localhost/test/index.php");

      Headers headers = firstResponse.headers();

      requestBuilder = requestBuilder.addHeader("Cookie", headers.get("Set-Cookie"));

      Request secondRequest = requestBuilder.get().build();
      Response secondResponse = _client.newCall(secondRequest).execute();
  }
}

看来,问题出在
\u cookie
对象上。

我在android上也面临同样的问题。你找到解决方案了吗?这个问题有更新吗?我也经历了这一点。