Zmieszany z różnymi metodami tworzenia plików cookie w HttpClient

Istnieją różne metody tworzenia plików cookie w HttpClient, jestem zdezorientowany, który z nich jest najlepszy. Muszę tworzyć, pobierać i modyfikować pliki cookie.

Na przykład mogę użyć następującego kodu, aby zobaczyć listę plików cookie i zmodyfikować je, ale jak je utworzyć?

Czy jest to właściwa metoda ich odzyskiwania? Potrzebuję ich, aby były dostępne we wszystkich klasach.

Ponadto metody, które znalazłem, zazwyczaj wymagają httpresponse, obiektów httprequest do wysyłania plików cookie do przeglądarki, ale co powiesz na to, jeśli nie chcę ich używać?

Kod

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class GetCookiePrintAndSetValue {

  public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    GetMethod method = new GetMethod("http://localhost:8080/");
    try{
      client.executeMethod(method);
      Cookie[] cookies = client.getState().getCookies();
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        System.err.println(
          "Cookie: " + cookie.getName() +
          ", Value: " + cookie.getValue() +
          ", IsPersistent?: " + cookie.isPersistent() +
          ", Expiry Date: " + cookie.getExpiryDate() +
          ", Comment: " + cookie.getComment());

        cookie.setValue("My own value");
      }
      client.executeMethod(method);
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}

ORAZ próbowałem utworzyć plik cookie przy użyciu następującego kodu, ale tak nie jest

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

....

public String execute() {
try{

     System.err.println("Creating the cookie");
     HttpClient httpclient = new HttpClient();
     httpclient.getParams().setParameter("http.useragent", "My Browser");

     GetMethod method = new GetMethod("http://localhost:8080/");
     httpclient.executeMethod(method);
     org.apache.commons.httpclient.Cookie cookie = new 
                                                org.apache.commons.httpclient.Cookie();
     cookie.setPath("/");
     cookie.setName("Tim");
     cookie.setValue("Tim");
     cookie.setDomain("localhost");
     httpclient.getState().addCookie(cookie);
     httpclient.executeMethod(method);
     System.err.println("cookie");

  }catch(Exception e){
     e.printStackTrace();
  }

Wydajność jest następujący, ale plik cookie nie zostanie utworzony.

SEVERE: Creating the cookie
SEVERE: cookie

Scenariusz

1)User has access to a form to search for products (example.com/Search/Products)
2)User fills up the form and submit it to class Search
3)Form will be submitted to Search class 
4)Method Products of Search class returns and shows the description of product        
  (example.com/Search/Products)
5)User clicks on "more" button for more description about product 
6)Request will be sent to Product class (example.com/Product/Description?id=4)
7)User clicks on "add to cookie" button to add the product id to the cookie 

Product class is subclasse of another class. So it can not extend any more class.

questionAnswers(2)

yourAnswerToTheQuestion