Using retrofit , I want to make a POST request http://milzinas.lt/oauthsilent/authorize . This URL is special because it redirects you to http://milzinas.e-bros.lt/oauthsilent/authorize . My retrofit setup uses OkHttpClient . If I make a request using OkHttpClient, then only the redirection works fine, that is, status code 401. However, when I use the same OkHttpClient with the modification, then the answer is status code 307. I think this has something to do with implementations of OkClient , which wraps OkHttpClient, but I'm not sure. Below is the code that I used to test this scenario. I use these libraries:
com.squareup.retrofit:retrofit:1.9.0 com.squareup.okhttp:okhttp:2.2.0
I understand that when a URL redirects you to another URL, the http client must fulfill two requests. In my case, the first request returns 307 (temporary redirect), and the second returns 401 (unauthorized). However, retrofitting always returns the response of the first request. Do you know how to make redirection correct? Maybe I could achieve this using another HTTP client? Any suggestions would be appreciated.
So, when I execute the code under console fingerprints
Retrofit failure. Status: 307 OkHttp. Status: 401
I want him to be
Retrofit failure. Status: 401 OkHttp. Status: 401
public class MainActivity extends AppCompatActivity { interface Api { @POST(URL) @Headers("Accept: application/json") void test(@Body Object dummy, Callback<Object> callback); } static final String BASE_URL = "http://milzinas.lt"; static final String URL = "/oauthsilent/authorize"; final OkHttpClient okHttpClient = new OkHttpClient(); Api api; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RestAdapter retrofit = new RestAdapter.Builder() .setEndpoint(BASE_URL) .setClient(new OkClient(okHttpClient)) .setConverter(new Converter() { @Override public Object fromBody(TypedInput body, Type type) throws ConversionException { return null; } @Override public TypedOutput toBody(Object object) { return null; } }) .build(); api = retrofit.create(Api.class); makeRequestOkHttp(); makeRequestRetrofit(); } void makeRequestOkHttp() { new AsyncTask<Object, Object, Object>() { @Override protected Object doInBackground(Object... objects) { try { Request request = new Request.Builder().url(BASE_URL + URL).build(); com.squareup.okhttp.Response response = okHttpClient.newCall(request).execute(); android.util.Log.d("matka", "OkHttp. Status: " + response.code()); } catch (IOException e) { throw new RuntimeException(e); } return null; } }.execute(); } void makeRequestRetrofit() { api.test("", new Callback<Object>() { @Override public void success(Object o, Response response) { android.util.Log.d("matka", "Retrofit success. Status: " + response.getStatus()); } @Override public void failure(RetrofitError error) { android.util.Log.d("matka", "Retrofit failure. Status: " + error.getResponse().getStatus()); } }); }
}
source share