How do you prevent Retrofit from automatically following a 302

前端 未结 5 1417
忘了有多久
忘了有多久 2021-02-18 23:43

I have an authentication call that i\'m trying to make using Retrofit on Android. The call returns a 302 to either a success or failure page. The original 302 response brings ba

5条回答
  •  故里飘歌
    2021-02-19 00:19

    to prevent the redirect you have to configure your client, e.g with OkHttp 2:

    private sendRequest() {
        OkHttpClient client = new OkHttpClient();
        client.setFollowRedirects(false);
    
        connectAdapter = new RestAdapter.Builder()
                .setClient(new OkClient(client))
                .setEndpoint("http://yourendpoint")
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();
    
        connectAdapter.create(YourRequest.class).sendMyRequest("login","password");
    
    }
    

    With OKHTTP 3 (you can read this answer from @gropapa):

    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.followRedirects(false);
    OkHttpClient httpClient = builder.build();
    

提交回复
热议问题