How to Maintain state Jersey Client Using java

无人久伴 提交于 2020-01-14 06:57:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!