I am trying to submit http POST request to a different server using Apache HttpClient 's PostMethod which giving following problem as soon as I execute. "HTTP/1.1 302 Moved Temporarily" Status Code: "302"
Could locate apache's example on conventional link. and my code is in accordance with what is present on most of the sites (that comes after googling).
here is the code i AM using:-
HttpMethod method = null; HttpClient httpClient = new HttpClient();
// Create a method instance. method = isPostRequest ? new PostMethod(urlToRequest) : new GetMethod(urlToRequest);
if(isPostRequest){ PostMethod post = (PostMethod)method; NameValuePair [] nvps = getParams(request.getParameterMap()); post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); post.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, 5000); post.setRequestBody(nvps); }
int statusCode = httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new PageServerException("Problem connecting to remote server: "+ method.getStatusLine() + ". Status Code: "+ method.getStatusCode()); } . . . _____________
Any pointers will be reaally helpful. Thanks and Regards, Jaideep
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
What is the value of "urlToRequest"? Is that a valid URL that does NOT redirect if you access it in the browser?
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
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
I'm not all that familiar with HttpClient, but I'd look into two issues:
What "Location" header is returned with the response (there should be one for a 302) ?
Is POST supposed to work with an empty body (it looks like that's what you're doing) ? [ April 15, 2008: Message edited by: Ulf Dittmer ]
Jaideep Pujara
Greenhorn
Joined: Jun 19, 2003
Posts: 11
posted
0
gentlemen,
to my surprise the problem stated for fixed & code above have started working after a minor change i.e. suffixing forward slash in case of POST request URL. ___________________
method = isPostRequest ? new PostMethod(urlToRequest+"/") : new GetMethod(urlToRequest); ___________________
Now code is working fine. Thanks alot for the involvement.