Reputation: 3260
I have been tryig to handle a redirect(302) in java code... And the code I have uses
org.apache.commons.httpclient.HttpClient
which doesn't declare setRedirectStrategy()
, so I have to write my own redirect implementation :
private void loadHttp302Request(HttpMethod method, HttpClient client,
int status, String urlString) throws HttpException, IOException {
if (status!=302)
return;
String[] url = urlString.split("/");
logger.debug("Method -> loadHttp302Request -> Location : " + method.getResponseHeader("Location")
.getValue());
logger.debug("Method -> loadHttp302Request -> Cookies : " + method.getResponseHeader("Set-Cookie")
.getValue());
logger.debug("Method -> loadHttp302Request -> Referrer : " + url[0]+"//"+url[2]);
HttpMethod theMethod = new GetMethod(urlString+method.getResponseHeader("Location")
.getValue());
theMethod.setRequestHeader("Cookie", method.getResponseHeader("Set-Cookie")
.getValue());
theMethod.setRequestHeader("Referrer",url[0]+"//"+url[2]);
int _status = client.executeMethod(theMethod);
logger.debug("Method -> loadHttp302Request -> Status : " + _status);
method = theMethod;
}
Once this is executed, the status code equals 200, so it seems like everything worked, but the response body and responsestream are both null. I've been able to sniff the TCP Stream with wireshark and as far as Wireshark is concerned, I receive back the whole response body from my redirect code... So I am not sure what I am doing wrong or what to look for next... Ideally It would be nice if I could use setRedirectStrategy()
, but because it is a Client's code :p I am stuck using the org.apache.commons.httpclient.HttpClient
...
I have debugged down the executeMethod()
and found that when reading the response from the input stream, it seems to receive nothing, even though wireshark most certainly shows that I received the full response body.
Any ideas would be appreciated :)
Upvotes: 4
Views: 4315
Reputation: 82966
method = theMethod;
at the end of loadHttp302Request
isn't going to do anything useful. When loadHttp302Request
returns, the method
object pointer in your calling (java) method is still going to be pointing to the original HttpMethod object.
Return theMethod
from loadHttp302Request
and get the response contents from that instead.
Upvotes: 1