Hello.
Please help with this situation.
You need to send requests to the server using regular HTTP. At the same time, the server uses the session mechanism for subsequent identification of requests (JSESSIONID || PHPSESSID - it does not matter.)
As a client, I tried to use OkHTTPClient and Retrofit 2. But all my attempts to throw this parameter there is a failure, because the server then sends back a new JSESSIONID.
An example of my actions
interface

@FormUrlEncoded @POST("client/signIn") Call<responseT2> loginPost(@Field("email") String email, @Field("pass") String pass, @Header("Set-Cookie") String string); //----------------------------------------------- в синглтоне создаю: client = new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .build(); retrofit = new Retrofit.Builder() .baseUrl(http://api.example.net/) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); 

Code for which I make a request

 beck = retrofit.create(BackEndApi.class); 

// ------------

 Call<responseT2> call = beck.loginPost( r.getEmail(), r.getPass(), NewNetUtil.getJSESSIOINID() ); call.enqueue(new Callback<responseT2>() { @Override public void onResponse(Call<responseT2> call, Response<responseT2> response) { NewNetUtil.setJSESSIOINID(response.headers().get("Set-Cookie")); } @Override public void onFailure(Call<responseT2> call, Throwable t) { Log.v("response", "ERORR"); t.printStackTrace(); } .build(); 

What I need to change or add to the server to accept each of my requests in one session.
I will be grateful ...

  • If he sends a new jsessionid, then you have returned some wrong one to him. What do you have for NewNetUtil.getJSESSIOINID / setJSESSIOINID? - Sergey
  • @Sergey - singleton - where granite JSESSIONID, so that you can "throw" it in requests - dimchuk
  • @Sergey - Checked if not to add - then Stetho (Developer tool) - does not show that such a cook is being sent - dimchuk
  • Can cookies somehow be stored incorrectly? There are no errors in getJSESSIOINID / setJSESSIOINID? - Sergey
  • @Sergey - getJSESSIOINID / setJSESSIOINID - there just get / set without modifications - dimchuk

1 answer 1

The problem is solved, it turned out to be a problem with the names of the cookies.

 Call<responseT2> loginPost(@Field("email") String email, @Field("pass") String pass, @Header("Set-Cookie") String string); 
  1. instead of Set-Cookie nada: Cookie or cookie
  2. the server sends Set-Cookie only once - at login, and sends an identifier inside this cookie.

My error was on the surface, with each request I read from headers

 NewNetUtil.setJSESSIOINID(response.headers().get("Set-Cookie")); 

You only need to count it for the first time, save it in the system, and then at each request, throw it into the header (Cookie or cookie) or in the constructor via @Header or via the Interceptor of requests in OkHttpClient.Builder ()