混淆了在HttpClient中创建cookie的不同方法

在HttpClient中创建cookie有不同的方法,我很困惑哪一个是最好的。 我需要创建,检索和修改cookie。

例如,我可以使用以下代码查看cookie列表并修改它们但是如何创建它们?

这是检索它们的正确方法吗? 我需要它们在所有类中都可访问。

  • 另外我发现的方法通常需要httpresponse,httprequest对象将cookie发送到浏览器,但如果我不想使用它们怎么样?

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(); } } } 

我尝试使用以下代码创建cookie,但事实并非如此

 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(); } 

输出如下,但不会创建cookie。

 SEVERE: Creating the cookie SEVERE: cookie 

脚本

 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. 

在第二个示例中,您将创建一个新的客户端cookie(即您正在模拟浏览器并将cookie发送到服务器 )。

这意味着您需要提供所有相关信息,以便客户端可以决定是否将cookie发送到服务器。

在您的代码中,您正确设置了路径,名称和值,但缺少信息。

 org.apache.commons.httpclient.Cookie cookie = new org.apache.commons.httpclient.Cookie(); cookie.setDomain("localhost"); cookie.setPath("/"); cookie.setName("Tim"); cookie.setValue("Tim"); 

如果您要实现的目的是将cookie发送到http服务器,则此方法有效

但是,你的第二个例子来自一个execute方法,因为你在你的标签中暗示了struts2,也许包含它的类可能是一个struts2 Action

如果是这种情况,您要实现的是将新cookie发送到浏览器

第一种方法是获取HttpServletResponse如下所示:

所以你的Action必须如下:

 public class SetCookieAction implements ServletResponseAware // needed to access the // HttpServletResponse { HttpServletResponse servletResponse; public String execute() { // Create the cookie Cookie div = new Cookie("Tim", "Tim"); div.setMaxAge(3600); // lasts one hour servletResponse.addCookie(div); return "success"; } public void setServletResponse(HttpServletResponse servletResponse) { this.servletResponse = servletResponse; } } 

可以使用CookieProviderInterceptor获取另一种方法(没有HttpServletResponse )。

struts.xml启用它

    ...  

然后实现CookieProvider

 public class SetCookieAction implements CookieProvider // needed to provide the coookies { Set cookies= new HashSet(); public Set getCookies() { return cookies; } public String execute() { // Create the cookie javax.servlet.http.Cookie div = new javax.servlet.http.Cookie("Tim", "Tim"); div.setMaxAge(3600); // lasts one hour cookies.put(cookie) return "success"; } } 

(归功于@RomanC指出这个解决方案)

如果您以后需要阅读它,您有两种选择:

  • Action实现ServletRequestAware并从HttpServletRequest读取cookie
  • 介绍一个CookieInterceptor并在你的Action实现CookiesAware ,方法setCookieMap允许读取cookie。

在这里您可以找到一些相关信息:

  • 使用Struts 2和Struts的cookie

首先,您可能不希望使用HttpClient,它是一个客户端(例如模拟浏览器)而不是服务器(可以创建cookie,用户的浏览器将存储)。

那就是说,我首先要解释你的第一个代码示例中发生了什么:

  • 您将GET请求发送到服务器
  • 服务器发送一些cookie以及它的响应(内容)
  • 您修改HttpClient实例中的cookie
  • 您将这些cookie发送回服务器
  • 服务器对您更改的cookie的处理完全取决于该服务器的编程内容
  • 在下一个请求时,服务器可能会将这些更改后的cookie发送回给您,正如我所说的那样取决于它的编程方式。

至于你的第二个例子:

  • 你创建一个cookie
  • 您在cookie上设置了一些信息
  • 您将cookie发送到服务器(在GET请求中)
  • 服务器现在可以使用您的cookie对其进行编程。 服务器可以忽略您的cookie (例如,不会将其发回给您)。

另一方面 ,您可能想要做的是编写一个Web应用程序 (例如服务器端),它创建cookie并根据客户端的输入(浏览器)更改它。

为此,您可以使用Servlet API。 一些事情:

 javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie("your cookie's name", "your cookie's value"); //response has type javax.servlet.http.HttpServletResponse response.addCookie(cookie); 

创建Web应用程序超出了简单的stackoverflow答案的范围 ,但Web中有许多好的教程。

如果用户应使用浏览器查看您的产品,则无法创建Web应用程序 。 创建一个不同的框架有不同的方法。 servlet API(例如HttpServletResponseHttpServletResponse )是最基本的。

我会说它(servlet API)也是你能找到解决这个问题的最简单的问题,尽管根本不容易开始使用web应用程序。