0

At the beginning i was sending a redirect with the cookies set on HttpServletResponse but later i decided to not redirect and only get the information gave from the servlet but the problem is i can't set the cookies on this post method.

So i would like to know how to set cookies with postmethod and if there is a way to process the cookies that are in HttpServletResponse

String temp=null;
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Oauth Data Requester");
    BufferedReader br = null;
    PostMethod method = new PostMethod(ADDRESS+"/SampleProvider");
    //Aqui ainda enviamos o XML inteiro como parametro
    method.addParameter("p", "\"java2s\"");
    try{
      int returnCode = client.executeMethod(method);
      if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
        System.err.println("no post method found");
      } else {
         temp=method.getResponseBodyAsString();
      }
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
      if(br != null) try { br.close(); } catch (Exception e) {}
    }
    return temp;
}

1 Answer 1

4

In servlets, you can get all cookies sent by the client using HttpServletRequest#getCookies().

Cookie[] cookies = request.getCookies();
// ...

And you can set cookies on the response using HttpServletResponse#addCookie().

response.addCookie(new Cookie(name, value));

In HttpClient 3.x (I assume that you're using 3.x, because the executeMethod() method isn't present on 4.x anymore), you can add cookies to the HttpState and then set it on HttpClient before executing the method.

HttpState state = new HttpState();
state.addCookie(new Cookie(".example.com", "name", "value"));
HttpClient client = new HttpClient();
client.setState(state);
// ...

After executing the method, you can get the (updated) cookies by

Cookie[] cookies = client.getState().getCookies();
// ...
Sign up to request clarification or add additional context in comments.

6 Comments

tks for the asw i ll tru it now :) stange i'm using 4.1.1 not 3... do u know any good tuturial about this area ?
Probably your classpath is polluted with a 3.x one and/or you didn't put 4.x in the classpath at all. As to the tutorials, there are several guides at HttpComponents site. hc.apache.org
you are right i have i have commons.httpclient 3.X on my libs :P is that bad ? should i go to 4.x ? by the way probably this import javax.servlet.http.Cookie; was giving this example problems i had to set the lib that i want to use state.addCookie(new Cookie(".example.com", "name", "value")); -> state.addCookie(new org.apache.commons.httpclient.Cookie(".example.com", "name", "value"));
It's not bad. It's just a newer version. As to the classname conflict, well, move it to its own class without any javax.servlet imports.
thx a lot for the help :) it's a bit late but tomorrow i ll try !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.