Skip to content

Support for setting cookies on 302 redirects #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/ning/http/client/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ public RequestBuilder setVirtualHost(String virtualHost) {
public RequestBuilder setFollowRedirects(boolean followRedirects) {
return super.setFollowRedirects(followRedirects);
}

@Override
public RequestBuilder addOrReplaceCookie(Cookie c) {
return super.addOrReplaceCookie(c);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/ning/http/client/RequestBuilderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,23 @@ private boolean allowBody(String method) {
return true;
}
}

public T addOrReplaceCookie(Cookie cookie) {
String cookieKey=cookie.getName();
boolean replace=false;
int index=0;
for(Cookie c : request.cookies) {
if(c.getName().equals(cookieKey)){
replace=true;
break;
};
index++;
}
if(replace) {
((ArrayList<Cookie>)request.cookies).set(index, cookie);
} else {
request.cookies.add(cookie);
}
return derived.cast(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,15 @@ public Object call() throws Exception {
final String newUrl = uri.toString();

log.debug("Redirecting to {}", newUrl);
for(String cookieStr : future.getHttpResponse().getHeaders(HttpHeaders.Names.SET_COOKIE)){
Cookie c=AsyncHttpProviderUtils.parseCookie(cookieStr);
nBuilder.addOrReplaceCookie(c);
}
for(String cookieStr : future.getHttpResponse().getHeaders(HttpHeaders.Names.SET_COOKIE2)){
Cookie c=AsyncHttpProviderUtils.parseCookie(cookieStr);
nBuilder.addOrReplaceCookie(c);
}


AsyncCallable ac = new AsyncCallable(future) {
public Object call() throws Exception {
Expand Down