Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   POSTing to a servlet (http://www.velocityreviews.com/forums/t955455-posting-to-a-servlet.html)

RVic 12-13-2012 01:59 PM

POSTing to a servlet
 
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.h andleResponse(BasicResponseHandler.java:68)
at org.apache.http.impl.client.BasicResponseHandler.h andleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:1070)
at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:1044)
at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:1035)
at ees.cmn.web.servlet.JSONServletTest.testFindOrgs(J SONServletTest.java:75)
at org.eclipse.ant.internal.launching.remote.EclipseD efaultExecutor.executeTargets(EclipseDefaultExecut or.java:32)
at org.eclipse.ant.internal.launching.remote.Internal AntRunner.run(InternalAntRunner.java:424)
at org.eclipse.ant.internal.launching.remote.Internal AntRunner.main(InternalAntRunner.java:138)


"Moved Temporarily?" What in the world might be going on here? TIA

RVince



Arne Vajhøj 12-14-2012 12:50 AM

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.h andleResponse(BasicResponseHandler.java:68)
> at org.apache.http.impl.client.BasicResponseHandler.h andleResponse(BasicResponseHandler.java:54)
> at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:1070)
> at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:1044)
> at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:1035)
> at ees.cmn.web.servlet.JSONServletTest.testFindOrgs(J SONServletTest.java:75)
> at org.eclipse.ant.internal.launching.remote.EclipseD efaultExecutor.executeTargets(EclipseDefaultExecut or.java:32)
> at org.eclipse.ant.internal.launching.remote.Internal AntRunner.run(InternalAntRunner.java:424)
> at org.eclipse.ant.internal.launching.remote.Internal AntRunner.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-...httpagent.html

Could you check the actual response getting back
with a tool like TCPMon?

Arne





Roedy Green 12-17-2012 10:46 AM

Re: POSTing to a servlet
 
On Thu, 13 Dec 2012 05:59:55 -0800 (PST), RVic <rvince99@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>org.apache.http.client.HttpResponseException: Moved Temporarily


Sounds like you got a 302. You can either chase the Location field
manually leg by leg till you get to where you want, or set some option
to ask the system to chase redirects for you without telling you about
them.

I do both in Brokenlinks. I don't use Apache though. I just use the
Sun classes wrapped in my own HTTP class, quite a bit simpler than
Apache.
See http://mindprod.com/products.html#BROKENLINKS

Watch what is happening with Wireshark. It should come clear quickly.
See http://mindprod.com/jgloss/wireshark.html
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.

Lothar Kimmeringer 12-30-2012 01:47 PM

Re: POSTing to a servlet
 
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");


[...]

> 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

[...]
> "Moved Temporarily?" What in the world might be going on here? TIA


Try
HttpPost httpPost = new HttpPost("http://myaerver.com:8080/servlets/json/");
instead.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!

Arne Vajhøj 12-30-2012 08:37 PM

Re: POSTing to a servlet
 
On 12/30/2012 8:47 AM, Lothar Kimmeringer wrote:
> 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");

>
> [...]
>
>> 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

> [...]
>> "Moved Temporarily?" What in the world might be going on here? TIA

>
> Try
> HttpPost httpPost = new HttpPost("http://myaerver.com:8080/servlets/json/");
> instead.


Good point.

Some servers do redirect with missing end slash.

OP should spend 5 seconds checking that.

Arne




All times are GMT. The time now is 06:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.