本文介绍了与改造一起使用时,OkHttp不会重定向POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 retrofit ,我要向 http://milzinas.lt/oauthsilent/authorize 发出POST请求.此URL很特殊,因为它会将您重定向到 http://milzinas.e-bros.lt/oauthsilent/authorize .我的改装设置使用 OkHttpClient .如果我仅使用OkHttpClient发出请求,则重定向可以正常工作,即收到401状态代码.但是,当我对改造使用相同的OkHttpClient时,响应为状态码307.我认为这与包装OkHttpClient的 OkClient 实现有关,但我不确定.下面是我用来测试这种情况的代码.我正在使用这些库:

Using retrofit I want to make POST request to 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 request using OkHttpClient only then redirecting works fine, i.e. 401 status code is received. However, when I use the same OkHttpClient with retrofit then response is status code 307. I think this has something to do with OkClient implementation which wraps the OkHttpClient but I'm not sure. Below is the code I used to test this scenario. I'm using these libraries:

com.squareup.retrofit:retrofit:1.9.0
com.squareup.okhttp:okhttp:2.2.0

我了解到,当URL将您重定向到另一个URL时,http客户端必须发出两个请求.在我的情况下,第一个请求返回307(临时重定向),第二个请求返回401(未经授权).但是,改造总是返回第一个请求的响应.您知道如何通过改型使重定向正常工作吗?也许我可以通过使用其他一些HTTP客户端来实现这一点?任何建议将不胜感激.

I understand that when URL is redirecting you to another URL the http client has to make two requests. In my case the first request returns 307 (Temporary Redirect) and the second one returns 401 (Unauthorized). However, retrofit always returns response of the first request. Do you know how to make redirecting work properly with retrofit? Maybe I could achieve this by using some other HTTP client? Any suggestions will be appreciated.

所以当我在控制台打印下执行代码时

So when I execute code below console prints

Retrofit failure. Status: 307
OkHttp. Status: 401

我希望成为

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());
        }
    });
}

}

推荐答案

即使在最新的v3.5.0中,问题仍然存在唯一可行的解​​决方法是 https://github.com/square/okhttp/issues/936#issuecomment-266430151

The problem persist even in the latest v3.5.0The only workaround that works ishttps://github.com/square/okhttp/issues/936#issuecomment-266430151

这篇关于与改造一起使用时,OkHttp不会重定向POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:26