My apologies for posting the question in hurry. I am re-stating my problem and code below.
I am trying to POST data over http using Apache's HttpClient. ie from
Tomcat on one machine to Jetty on another, And getting the response back to Tomcat.
The code below is working fine for GET requests but posing problem (Error 302, HTTP/1.1 302 Moved Temporarily) with post requests.
The call "httpClient.executeMethod(method);" dont reach to target
servlet's doPost() method when using PostMethod, whereas it cleanly reach doGet() of target Servlet when using GetMethod.
All the variables have appropriate values (ie urlToRequest = "http://<hostname>:<port>/<appcontext>" ) in the code below.
My explicit question could be why I am getting REDIRECT code at all? My target app is NOT redirecting.
Debugging clearly shows POST requests never reached target URL.
Probably I am missing some setting/(usage scenario) in API.
_______________________________________
boolean isPostRequest = "POST".equals(request.getMethod());
HttpClient httpClient = new HttpClient();
// Create a method instance.
HttpMethod method = isPostRequest ? new PostMethod(urlToRequest) : new GetMethod(urlToRequest);
if(isPostRequest){
PostMethod post = (PostMethod)method;
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
post.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, 5000);
post.setRequestBody(paramsToPass.toArray(new NameValuePair[]{}));
}else{
method.setQueryString(paramsToPass.toArray(new NameValuePair[]{}));
}
// Execute the method.
int statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new PageServerException("Problem connecting to remote server: "+ method.getStatusLine() + ". Status Code: "+ method.getStatusCode());
}
String contentType = method.getResponseHeader("Content-Type").getValue();
if(contentType.contains("application")){
body = method;
}else{
body = getProcessedBody(method.getResponseBodyAsString());
method.releaseConnection();
}
_____________________________________________
Also, the code samples provided by Apache (see following code snippet), does not help......just shows same behavior.
___________
// Usually a successful form-based login results in a redicrect to
// another url
int statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
System.exit(1);
}
}
_______________________
My POST requests are NOT redirect oriented. They are straight request-response types.
ThankYou Mr Dittmer and Joe, for your replies so far.
Any further insight will be helpful.
Regards,
Jaideep