2
我正在用泽西岛Java库编写客户端应用程序,我不会成功地向服务器发送cookie。我的错误在哪里?这里是我的代码:泽西岛 - 在客户端上设置的cookie不会到达服务器
客户:
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(getUrl());
//Create invocation builder
Invocation.Builder invocationBuilder = webTarget.request(getMime_type());
//Set the cooky
invocationBuilder = invocationBuilder.cookie("mioCookie","value of my cookie");
Response response = invocationBuilder.get();
SERVER:
@Path("/file")
public class FileService {
Logger LOG=Logger.getLogger(this.getClass().getName());
private static final String FILE_PATH = "file.log";
@GET
@Path("/get")
@Produces("text/plain")
public Response getFile(@CookieParam(value = "mioCookie") String mioCookie) {
LOG.info("***** Cookie in input: " + mioCookie);
File file = new File(FILE_PATH);
NewCookie modifiedCookie = new NewCookie("mioCookie", mioCookie + "- modified");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename=\"file_from_server.log\"");
response.cookie(modifiedCookie);
return response.build();
}
什么是日志写的是:
infos: ***** Cookie in input: null.
我在哪里错了?
您是否尝试使com.sun.jersey.spi.container.ContainerRequestFilters过滤器登录传入的请求?感谢这一点,您将知道问题出在客户端还是服务器代码中。 –
@Adam这是一个很好的建议,这也可以使用网络调试工具来检测互联网流量(例如http://fiddler2.com/) – LeastOne
刚刚用Jersey 2.5(客户端)和Jersey 1.17(服务器)测试了您的代码。它的工作原理:'*****输入Cookie:我的cookie的值 –