Menu

#129 WebClient needs putCookie/getCookie

closed
None
5
2014-08-26
2008-05-16
No

A feature request:

For JSFUnit, we create the WebClient with a factory. This is because the WebClient needs to be initialized with two cookies and the proper JSFUnit environment needs to be set up.

So a user of JSFUnit will say:

WebClient client = WebClientFactory.makeWebClient();

Inside makeWebClient(), I set the two cookies like this:

webClient.addRequestHeader("Cookie", "JSESSIONID=" + session.getId() + "; " + JSF_UNIT_CONVERSATION_FLAG + "=" + JSF_UNIT_CONVERSATION_FLAG);

It would be cleaner to say:
webClient.putCookie("JSESSIONID", session.getId());
webClient.putCookie("JSF_UNIT_CONVERSATION_FLAG", "JSF_UNIT_CONVERSATION_FLAG");

As it is now, I can't add two headers named "Cookie" (which should be allowed), so I have to do it all in one line of code.

But the real problem comes in when a user wants to add his own cookie before the first request. As it is now, there is no way to update the cookies already set up in the factory. I don't see any way to even read back the cookies that were set. So the user can't even do a read/update/overwrite. He's stuck.

A nice solution would be to add putCookie/getCookie methods to WebClient.

Discussion

  • Marc Guillemot

    Marc Guillemot - 2008-05-16

    Logged In: YES
    user_id=402164
    Originator: NO

    It is already possible to manage the cookies through
    webClient.getWebConnection().getState()

    but you're right, it would probably make sense to provide something like a CookieManager to make this easier. I change this issue as feature request.

     
  • Stan Silvert

    Stan Silvert - 2008-05-16

    Logged In: YES
    user_id=1039639
    Originator: YES

    Both getWebConnection() and getState() are documented as "INTERNAL API - SUBJECT TO CHANGE". So I would be reluctant to use that.

    Also, does getWebConnection().getState() return a non-null value before any request is made? We want to be able to set the cookies before the first request is made.

     
  • Ahmed Ashour

    Ahmed Ashour - 2008-06-19

    Logged In: YES
    user_id=950730
    Originator: NO

    Well, this is not directly related to WebClient, but to the WebRequestSettings and WebResponse.

    I believe webClient.addRequestHeader adds the cookie 'before' the first request.

    Also, to get the cookies, you can use: page.getWebResponse().getResponseHeaderValue("Cookie");

     
  • Stan Silvert

    Stan Silvert - 2008-06-20

    Logged In: YES
    user_id=1039639
    Originator: YES

    But for that I have to build my own single cookie string for all the cookies I want to set. I tried to do:

    webClient.addRequestHeader("Cookie", "cookie1=value");
    webClient.addRequestHeader("Cookie", "cookie2=value");

    But the first cookie was overwritten by the second. Instead, it should create two headers like this:
    Cookie: cookie1=value
    Cookie: cookie2=value

    A nicer cookie API as I suggested would be good, but at the very least, I think that addRequestHeader() needs to be fixed.

     
  • Marc Guillemot

    Marc Guillemot - 2008-06-20

    Logged In: YES
    user_id=402164
    Originator: NO

    Just as note concerning the cookie headers: how they are transmitted (whereas as single header line or as multiple header lines) depends on the cookie policy.

     
  • Daniel Gredler

    Daniel Gredler - 2008-07-22

    Logged In: YES
    user_id=1109422
    Originator: NO

    I need to do a bit more research on the APIs in this area before I feel comfortable addressing this issue. We seem to have a lot of internal APIs exposed here, and I want to make sure I'm not just hacking over a hack, which will then require another hack, etc.

    One note regarding our cookie headers: we have to use a single cookie header because some servers and/or web apps don't work with separate headers for each cookie. It took me a long time to come up with a default cookie config that works in most (all?) cases, and was the result of an extensive real-world web scraping project I worked on a while back.

    @Marc: This means we can release 2.2 without closing this issue.

     
  • Daniel Gredler

    Daniel Gredler - 2008-09-09

    CookieManager class added in SVN. Enjoy!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.