[英]How to Maintain state Jersey Client Using 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.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
看看这是否有帮助。这段代码为我提供了一个模板。希望它对你有用。
您首先需要从初始响应中获取 cookie。 这些将是NewCookie
实例,它们是服务器发送给客户端Set-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();
也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.