Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?

大憨熊 提交于 2020-01-02 03:10:14

问题


I'm using Square's Retrofit Client to make simple requests from an Android App. Like so:

  RestAdapter restAdapter = new RestAdapter.Builder()
            .setServer(Configurations.getInstance().plistMap.get("PTBaseURL"))
            .setRequestHeaders(new RequestHeaders() {
                @Override
                public List<Header> get() {
                    List<Header> headers = new ArrayList<Header>();
                        Header authHeader = new Header("Authorization", authType + " " + UserManager.getInstance().currentUser.token);
                        headers.add(authHeader);
                    }
                    return headers;
                }
            })
            .build();

    this.service = restAdapter.create(ClientInterface.class);

One endpoint redirects to a different URL (s3). For an reason not important to this question the redirect request is failing and thus my callback.failure(error) method is being called. I need to be able to access and modify the redirect URL or request at some point, preferable in callback.failure(). How can I do this?

Alternatively, is there a way to set something like followRedirects = false (and intercept the redirect in this way)?


回答1:


With OkHttp:

 public static void setFollowRedirects (boolean auto)
 public OkHttpClient setFollowProtocolRedirects(boolean followProtocolRedirects)

With HttpURLConnection:

public static void setFollowRedirects (boolean auto)
public void setInstanceFollowRedirects (boolean followRedirects)

See discussion here.



来源:https://stackoverflow.com/questions/18218726/square-retrofit-client-how-to-enable-disable-followredirects-how-to-intercept

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!