问题
How to Maintain state Jersey Client
{ //
// 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.
回答1:
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();
See also:
- javax.ws.rs.core.Cookie vs javax.ws.rs.core.NewCookie , What is the difference?
来源:https://stackoverflow.com/questions/46877214/how-to-maintain-state-jersey-client-using-java