如何使用java维护状态Jersey客户端 [英] How to Maintain state Jersey Client Using java
问题描述
如何维护州泽西客户
{ //
// 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
看看这是否有帮助。此代码为我提供了一个模板。希望对你有帮助。
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 , What is the difference?
这篇关于如何使用java维护状态Jersey客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!