POSTing to a servlet

Discussion in 'Java' started by RVic, Dec 13, 2012.

  1. RVic

    RVic Guest

    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
    RVic, Dec 13, 2012
    #1
    1. Advertising

  2. RVic

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Dec 14, 2012
    #2
    1. Advertising

  3. RVic

    Roedy Green Guest

    On Thu, 13 Dec 2012 05:59:55 -0800 (PST), RVic <>
    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.
    Roedy Green, Dec 17, 2012
    #3
  4. 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:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Dec 30, 2012
    #4
  5. RVic

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Dec 30, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alanb
    Replies:
    2
    Views:
    517
    alanb
    Apr 23, 2004
  2. Matthias Krueger
    Replies:
    3
    Views:
    29,109
    Matthias Krueger
    Sep 8, 2003
  3. ppcdev
    Replies:
    0
    Views:
    479
    ppcdev
    Jan 13, 2004
  4. circuit_breaker
    Replies:
    2
    Views:
    1,989
    Jack Jia
    Apr 4, 2004
  5. AmitKu
    Replies:
    7
    Views:
    485
    John Timney \(MVP\)
    Jan 8, 2007
Loading...

Share This Page