我正在使用jersey-client 1.19.4来测试我的web应用程序。如果我用邮递员进行测试,我可以在‘发送’操作之后找到cookie "JSESSIONID“。我可以找到“jsessionid=.”在ClientResponse.toString()中,但是ClientResponse.getCookies()什么也不返回。
WebResource webResource = client.resource(someUrl);
FormDataMultiPart formData = new FormDataMultiPart();
formData.bodyPart(new FormDataBodyPart("userId", userId));
formData.bodyPart(new FormDataBodyPart("uPasswd", uPasswd));
ClientResponse response = webResource.accept("*/*").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, formData);
System.out.println("response: " + response.toString()); // 'jsessionid' found here
List<NewCookie> cookies = response.getCookies();
System.out.println("# of cookies: " + cookies.size()); // prints "# of cookies: 0"如何从ClientResponse获得"JSESSIONID“?
发布于 2018-07-31 09:14:58
JSESSIONID可以用几种不同的方式设置。根据JSR-000315 JavaServlet3.0最终发行版第7.1章会议跟踪机制,可以使用以下方法:
在您的示例中,似乎正在使用URL重写。最常见的两个原因是:
因为您在使用Postman时获得了cookie,这很可能意味着您的泽西客户端不处理cookie。一种使用jersey-apache-client 根据这个答案将其与Apache 根据这个答案集成的方法。
发布于 2018-07-31 09:16:30
在邮寄名单中:
http://jersey.576304.n2.nabble.com/Session-Handling-not-working-with-Jersey-Client-td4519663.html
默认情况下,泽西客户端使用不使用的HttpURLConnection。 支持cookie管理(因此支持会话)。 您需要切换到使用Apache客户端支持。 然后将以下属性设置为true:曲奇饼 .setProperty("com.sun.jersey.impl.client.httpclient.handleCookies",DefaultApacheHttpClientConfig = DefaultApacheHttpClientConfig();ApacheHttpClient c=ApacheHttpClient.create(DefaultApacheHttpClientConfig); 此外,还可以对Apache客户端使用授权 集成。 DefaultApacheHttpClientConfig config =新DefaultApacheHttpClientConfig();config.getState().setCredentials(null,null,-1,"foo","bar");ApacheHttpClient c= ApacheHttpClient.create(config);WebResource r=c.resource(“http://host/base"”);String s= r.get(String.class);s= r.post(String.class,s);
https://stackoverflow.com/questions/51608688
复制相似问题