如何使用java维护状态Jersey客户端

编程入门 行业动态 更新时间:2024-10-27 22:31:12
本文介绍了如何使用java维护状态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"); 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

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

See if this Helps.This code provides me a template.hope it works for you.

推荐答案

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

You first need to get the cookies from the initial response. These will be NewCookie instances, which are the cookies that the server sends to the client, as Set-Cookie headers. On the client side, you need to send a Cookie back to the server, which will result in a Cookie header being sent. If you try to set a NewCookie on the client, it would set a Set-Cookie header, which would be wrong. You can easily convert a NewCookie to a Cookie simply by calling newCookie.toCookie()

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

另请参阅:

  • javax.ws.rs.core.Cookie vs javax.ws.rs.core.NewCookie ,有什么区别?
  • javax.ws.rs.core.Cookie vs javax.ws.rs.core.NewCookie , What is the difference?

更多推荐

如何使用java维护状态Jersey客户端

本文发布于:2023-11-17 14:13:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   客户端   状态   java   Jersey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!