Java okHttp重用保活连接
Java okHttp reuse keep-alive connection
如何通过 okHttp 重用 http keep-alive 连接?
我的代码示例:
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();
}
}
看来,问题出在 _cookie
对象上。
如何通过 okHttp 重用 http keep-alive 连接?
我的代码示例:
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();
}
}
看来,问题出在 _cookie
对象上。