简体   繁体   English

阻止HttpURLConnection发送cookie

[英]Prevent HttpURLConnection from sending cookie

I have the following code: 我有以下代码:

URL uri = new URL(my_service_url);
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("content-type", "application/x-www-form-url-encoded");
connection.connect();

// I process request here

It works ok most of the time, it is just sometimes the request has some headers I don't set, specifically some cookies like: "Cookie: JSESSIONID=XXXX". 它在大多数情况下都可以正常运行,只是有时请求中包含一些我未设置的标头,尤其是一些Cookie,例如:“ Cookie:JSESSIONID = XXXX”。 In my case this causes problems and my request sometimes fails because of this. 以我为例,这会导致问题,因此我的请求有时会失败。

How can I make sure my request do not sent other cookies I don't set explicitly? 如何确保我的请求没有发送其他未明确设置的Cookie?

您可以通过将其显式设置为空来避免发送Cookie

connection.setRequestProperty("Cookie","");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM