gpt4 book ai didi

java - Jersey - 客户端上设置的 cookie 未到达服务器

转载 作者:行者123 更新时间:2023-11-30 11:24:18 26 4
gpt4 key购买 nike

我正在用 jersey java 库编写一个客户端应用程序,我没有成功将 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();

服务器:

@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.

我哪里错了?

最佳答案

您可以在调用时简单地添加 Response response = target.request().cookie(Your Cookie)。不要显式调用构建器,因为它不会在您的最终调用中添加 cookie。这应该将 cookie 添加到您的调用中。为了更清楚,添加了一个帖子调用示例响应 response =webTarget.request().accept(MediaType.APPLICATION_JSON).cookie(cookie).buildPost(Entity.entity(jsonObject.toString(), MediaType.APPLICATION_JSON)).invoke();希望有帮助

关于java - Jersey - 客户端上设置的 cookie 未到达服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20707626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com