Skip to content

Commit

Permalink
HTTPClient: customizable follow redirects.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanchenko committed Mar 13, 2014
1 parent 8e2c8c7 commit fe277c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions droidparts/src/org/droidparts/net/http/RESTClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public void setCookieCacheEnabled(boolean enabled, boolean persistent) {
getWorker().setCookieJar(enabled ? cookieJar : null);
}

public void setFollowRedirects(boolean follow) {
getWorker().setFollowRedirects(follow);
}

public void setHeader(String key, String value) {
getWorker().setHeader(key, value);
}
Expand Down
5 changes: 5 additions & 0 deletions droidparts/src/org/droidparts/net/http/worker/HTTPWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void throwIfNetworkOnMainThreadException(Exception e) {
protected static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;

protected final HashMap<String, String> headers = new HashMap<String, String>();
protected boolean followRedirects = true;

public final void authenticateBasic(String user, String password) {
String val = null;
Expand All @@ -54,6 +55,10 @@ public final void setHeader(String key, String val) {
}
}

public void setFollowRedirects(boolean follow) {
this.followRedirects = follow;
}

public abstract void setCookieJar(CookieJar cookieJar);

protected static final boolean isErrorResponseCode(int responseCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ public HttpClientWorker(String userAgent) {
HttpProtocolParams.setUserAgent(params, userAgent);
}
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpClientParams.setRedirecting(params, false);
setFollowRedirects(followRedirects);
HttpConnectionParams.setConnectionTimeout(params,
SOCKET_OPERATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
HttpConnectionParams.setSocketBufferSize(params, BUFFER_SIZE);
HttpClientParams.setCookiePolicy(params, BROWSER_COMPATIBILITY);
}

@Override
public void setFollowRedirects(boolean follow) {
HttpClientParams
.setRedirecting(httpClient.getParams(), followRedirects);
}

@Override
public void setCookieJar(CookieJar cookieJar) {
httpClient.setCookieStore(cookieJar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public HttpURLConnection getConnection(String urlStr, String requestMethod)
}
conn.setRequestProperty(ACCEPT_ENCODING, "gzip,deflate");
conn.setRequestMethod(requestMethod);
conn.setInstanceFollowRedirects(followRedirects);
if (Method.PUT.equals(requestMethod)
|| Method.POST.equals(requestMethod)) {
conn.setDoOutput(true);
Expand Down

0 comments on commit fe277c0

Please sign in to comment.