【问题标题】:How to Maintain state Jersey Client Using java如何使用 java 维护状态 Jersey 客户端
【发布时间】:2017-10-24 02:37:18
【问题描述】:

如何维护状态 Jersey 客户端

    { //
      //      Some session logic 
      //
     } 
        Client client = ClientBuilder.newClient();
        WebTarget baseTarget =
        client.target("https");
        /
        MultivaluedMap<String, String> formData = new 
        MultivaluedHashMap<String, String>();
        formData.add("usr", "@gmail.com");
        formData.add("pwd", "mat");
        Response response = baseTarget.request().post(Entity.form(formData))  
        System.out.println("----Second-time--method invoked GET-------");

        Response resp_sec = base2Target.request().get(); //second time in session perform action client side

看看这是否有帮助。这段代码为我提供了一个模板。希望它对你有用。

【问题讨论】:

  • 我希望你没有在这里写你的真实登录密码!
  • 我已经更改了我的凭据,安全性。

标签: java session cookies get jersey


【解决方案1】:

您首先需要从初始响应中获取 cookie。这些将是 NewCookie 实例,它们是服务器发送给客户端的 cookie,作为 Set-Cookie 标头。在客户端,您需要将Cookie 发送回服务器,这将导致发送Cookie 标头。如果您尝试在客户端上设置NewCookie,它将设置Set-Cookie 标头,这是错误的。您只需调用newCookie.toCookie() 即可轻松地将NewCookie 转换为Cookie

Map<String, NewCookie> cookies = response.getCookies();
Invocation.Builder ib = baseTarget.request();
for (NewCookie cookie: cookies.values()) {
    ib.cookie(cookie.toCookie());
}
Response response = ib.get();

另请参阅:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2017-01-17
  • 2011-04-23
  • 1970-01-01
  • 2013-12-22
  • 2010-11-29
相关资源
最近更新 更多