Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20291 > unrolled thread

POSTing to a servlet

Started byRVic <rvince99@hotmail.com>
First post2012-12-13 05:59 -0800
Last post2012-12-30 15:37 -0500
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  POSTing to a servlet RVic <rvince99@hotmail.com> - 2012-12-13 05:59 -0800
    Re: POSTing to a servlet Arne Vajhøj <arne@vajhoej.dk> - 2012-12-13 19:50 -0500
    Re: POSTing to a servlet Roedy Green <see_website@mindprod.com.invalid> - 2012-12-17 02:46 -0800
    Re: POSTing to a servlet Lothar Kimmeringer <news200709@kimmeringer.de> - 2012-12-30 14:47 +0100
      Re: POSTing to a servlet Arne Vajhøj <arne@vajhoej.dk> - 2012-12-30 15:37 -0500

#20291 — POSTing to a servlet

FromRVic <rvince99@hotmail.com>
Date2012-12-13 05:59 -0800
SubjectPOSTing to a servlet
Message-ID<e3d17f12-5b5f-4655-a596-402d66bcbf82@googlegroups.com>
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

RVince

[toc] | [next] | [standalone]


#20314

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-12-13 19:50 -0500
Message-ID<50ca77c4$0$282$14726298@news.sunsite.dk>
In reply to#20291
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



[toc] | [prev] | [next] | [standalone]


#20396

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-12-17 02:46 -0800
Message-ID<bpttc81it3k0b4gp0a2lhc5kqam9380e3b@4ax.com>
In reply to#20291
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. 

[toc] | [prev] | [next] | [standalone]


#20816

FromLothar Kimmeringer <news200709@kimmeringer.de>
Date2012-12-30 14:47 +0100
Message-ID<fxzo0zi1mxg0.dlg@kimmeringer.de>
In reply to#20291
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!

[toc] | [prev] | [next] | [standalone]


#20827

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-12-30 15:37 -0500
Message-ID<50e0a61e$0$295$14726298@news.sunsite.dk>
In reply to#20816
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web