RegistrationBody.class:
public class RegistrationBody {
public String email;
public String first_name;
public String last_name;
public String password;
}
RegistrationResponce.class:
public class RegistrationResponse {
public String cookie;
}
Запрос:
@POST("/signup/")
Call<RegistrationResponse> registerUser(@Body RegistrationBody registrationBody);
Для авторизации пользователя я передаю POST-запросом поля email, first_name, last_name, password. Этот запрос должен возвратить мне Set-cookie, но не возвращает:
requestInterface = Controller.getApi();
RegistrationBody registrationBody = new RegistrationBody();
registrationBody.email = email;
registrationBody.first_name = firstName;
registrationBody.last_name = lastName;
registrationBody.password = newPassword;
requestInterface.registerUser(registrationBody).enqueue(new Callback<RegistrationResponse>() {
@Override
public void onResponse(Call<RegistrationResponse> call, Response<RegistrationResponse> response) {
Log.d("myLogs", "Response" + response.raw().headers());
}
@Override
public void onFailure(Call<RegistrationResponse> call, Throwable t) {
Log.d("myLogs", "response onFailure");
Log.d("myLogs", "Throwable: " + t.toString());
}
});
AddCookiesInterceptor.class:
public class AddCookiesInterceptor implements Interceptor {
public static final String PREF_COOKIES = "PREF_COOKIES";
private Context context;
public AddCookiesInterceptor(Context context) {
this.context = context;
}
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
HashSet<String> preferences = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_COOKIES, new HashSet<String>());
String cookiestring = "";
for (String cookie : preferences) {
String[] parser = cookie.split(";");
cookiestring = cookiestring + parser[0] + "; ";
}
builder.addHeader("Cookie", cookiestring);
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
}
return chain.proceed(builder.build());
}
}
ReceivedCookiesInterceptor.class:
public class ReceivedCookiesInterceptor implements Interceptor {
private Context context;
public ReceivedCookiesInterceptor(Context context) {
this.context = context;
}
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet("PREF_COOKIES", new HashSet<String>());
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
SharedPreferences.Editor memes = PreferenceManager.getDefaultSharedPreferences(context).edit();
memes.putStringSet("PREF_COOKIES", cookies).apply();
memes.commit();
}
return originalResponse;
}
}
Добавила клиент:
private static Context context;
public Controller (Context context) {
this.context = context;
}
public static RequestInterface getApi() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new AddCookiesInterceptor(Controller.context));
builder.addInterceptor(new ReceivedCookiesInterceptor(context));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
return requestInterface;
Как исправить код, чтобы запрос возвратил мне Set-cookie? Запрос выводит следующее:
Server: nginx/1.10.3
Date: Tue, 28 Nov 2017 12:49:34 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 17
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS, DELETE
Access-Control-Allow-Origin:
Использование следующего кода выдает ту же информацию:
private final OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("email", intent.getStringExtra("email"))
.add("first_name", intent.getStringExtra("firstName"))
.add("last_name", intent.getStringExtra("lastName"))
.add("password", etNewPassword.getText().toString())
.build();
Request request = new Request.Builder()
.url("http://api.yogamy.live/signup")
.post(formBody)
.build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.d("myLogs", "response onFailure");
Log.d("myLogs", "Throwable: " + e.toString());
}
@Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
Log.d("myLogs", "Responce " + response.headers());
}
});