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的值 –