Java – square retro fit client: how to enable / disable followredirects? How do I intercept redirected URLs?
•
Java
I'm using square's Retro fit client to make a simple request from an Android application like this:
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);
An endpoint redirects to a different URL (S3) The redirect request failed for a reason that is not important to this problem, so my callback The failure (error) method is called I need to be able to access and modify the redirect URL or request at some time, preferably in callback In failure() How can I do this?
Or, is there a way to set follow redirects = false (and intercept redirects in this way)?
Solution
Using okhttp:
public static void setFollowRedirects (boolean auto) public OkHttpClient setFollowProtocolRedirects(boolean followProtocolRedirects)
Use httpurlconnection:
public static void setFollowRedirects (boolean auto) public void setInstanceFollowRedirects (boolean followRedirects)
See discussion here
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码