session - Java okHttp reuse keep-alive connection -
how reuse http keep-alive connection via okhttp?
my code sample:
public class mainwithokhttp { static okhttpclient _client = new okhttpclient(); public static void main(string[] args) { ... query1(); ... // in request request _request = new request.builder() .url("...") .addheader("connection", "keep-alive") .addheader("cookie", _cookie) .post(postparams) .build(); // in request previous session closed, why? // 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(); ... } ... }
so, i'm calling query1()
method. in method connection opened, session in server side created , cookie sessionid received.
but, when i'm performing query server - previous connection not used , session on server closed. session life time in server not small, problem not in life time.
ps: i'm getting captcha server , recognizing captcha code, performing query captcha code server. captcha in server side not recognized, because session closed , captcha code stored in session.
eventually, following code working me:
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(); } }
it seems, problem _cookie
object.
Comments
Post a Comment