HttpURLConnection with a non system-wide CookieHandler
I have a web application that makes HTTP requests using HttpURLConnection
. I need it to handle cookies. I know that it's easily done by adding just one line of code, something like
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
The problem is this way I'm setting the system-wide cookie handler as the documentation describes. This also affects other web applications that run in the same servlet container. For example if I want CookiePolicy.ACCEPT_ORIGINAL_SERVER
in one application and CookiePolicy.ACCEPT_ALL
in another, it won't work.
Is there a way to have a CookieHandler
that is only used by a single HttpURLConnection
instance?
java cookies httpurlconnection cookiemanager
add a comment |
I have a web application that makes HTTP requests using HttpURLConnection
. I need it to handle cookies. I know that it's easily done by adding just one line of code, something like
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
The problem is this way I'm setting the system-wide cookie handler as the documentation describes. This also affects other web applications that run in the same servlet container. For example if I want CookiePolicy.ACCEPT_ORIGINAL_SERVER
in one application and CookiePolicy.ACCEPT_ALL
in another, it won't work.
Is there a way to have a CookieHandler
that is only used by a single HttpURLConnection
instance?
java cookies httpurlconnection cookiemanager
Maybe this is a solution for you? stackoverflow.com/questions/16305486/…
– chromanoid
Nov 29 '18 at 16:43
@chromanoid No, the solutions from it only help to separate cookies between different threads.
– John29
Nov 29 '18 at 20:07
add a comment |
I have a web application that makes HTTP requests using HttpURLConnection
. I need it to handle cookies. I know that it's easily done by adding just one line of code, something like
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
The problem is this way I'm setting the system-wide cookie handler as the documentation describes. This also affects other web applications that run in the same servlet container. For example if I want CookiePolicy.ACCEPT_ORIGINAL_SERVER
in one application and CookiePolicy.ACCEPT_ALL
in another, it won't work.
Is there a way to have a CookieHandler
that is only used by a single HttpURLConnection
instance?
java cookies httpurlconnection cookiemanager
I have a web application that makes HTTP requests using HttpURLConnection
. I need it to handle cookies. I know that it's easily done by adding just one line of code, something like
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
The problem is this way I'm setting the system-wide cookie handler as the documentation describes. This also affects other web applications that run in the same servlet container. For example if I want CookiePolicy.ACCEPT_ORIGINAL_SERVER
in one application and CookiePolicy.ACCEPT_ALL
in another, it won't work.
Is there a way to have a CookieHandler
that is only used by a single HttpURLConnection
instance?
java cookies httpurlconnection cookiemanager
java cookies httpurlconnection cookiemanager
asked Nov 23 '18 at 18:10
John29John29
1,88822033
1,88822033
Maybe this is a solution for you? stackoverflow.com/questions/16305486/…
– chromanoid
Nov 29 '18 at 16:43
@chromanoid No, the solutions from it only help to separate cookies between different threads.
– John29
Nov 29 '18 at 20:07
add a comment |
Maybe this is a solution for you? stackoverflow.com/questions/16305486/…
– chromanoid
Nov 29 '18 at 16:43
@chromanoid No, the solutions from it only help to separate cookies between different threads.
– John29
Nov 29 '18 at 20:07
Maybe this is a solution for you? stackoverflow.com/questions/16305486/…
– chromanoid
Nov 29 '18 at 16:43
Maybe this is a solution for you? stackoverflow.com/questions/16305486/…
– chromanoid
Nov 29 '18 at 16:43
@chromanoid No, the solutions from it only help to separate cookies between different threads.
– John29
Nov 29 '18 at 20:07
@chromanoid No, the solutions from it only help to separate cookies between different threads.
– John29
Nov 29 '18 at 20:07
add a comment |
1 Answer
1
active
oldest
votes
In standard oracle implementation the HttpURLConnection
get the default CookieHandler
on the constructor, so this is one possible solution. Create a synchronized singleton factory that create the HttpURLConnections
using a specific manager for each application. Not good idea in my opinion.
Other bad idea is provide your own CookiePolicy
and do the trick on the shouldAccept
method.
Or you can manually control cookies on the app that should not share the CookieHandler
:
HttpURLConnection firstCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
firstCall.connect();
List<HttpCookie> cookieList = HttpCookie.parse(firstCall.getHeaderField("Set-Cookie"));
firstCall.disconnect();
StringBuilder cookies = new StringBuilder();
for(HttpCookie cookie:cookieList) {
//if(cookie.SOME_VALIDATION) {
if(cookies.length() > 0) {
cookies.append("; ");
}
cookies.append(cookie.toString());
//}
}
HttpURLConnection secondCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
secondCall.setRequestProperty("Cookie", cookies.toString());
secondCall.connect();
//dosomething
secondCall.disconnect();
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can usesetInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it withHttpURLConnection
, but thanks for the answer anyway.
– John29
Nov 27 '18 at 15:38
There is no Parameter to changeCookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.
– fhofmann
Nov 28 '18 at 7:10
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch fromHttpURLConnection
because I already have a lot of code specific to it.
– John29
Nov 28 '18 at 16:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451280%2fhttpurlconnection-with-a-non-system-wide-cookiehandler%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In standard oracle implementation the HttpURLConnection
get the default CookieHandler
on the constructor, so this is one possible solution. Create a synchronized singleton factory that create the HttpURLConnections
using a specific manager for each application. Not good idea in my opinion.
Other bad idea is provide your own CookiePolicy
and do the trick on the shouldAccept
method.
Or you can manually control cookies on the app that should not share the CookieHandler
:
HttpURLConnection firstCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
firstCall.connect();
List<HttpCookie> cookieList = HttpCookie.parse(firstCall.getHeaderField("Set-Cookie"));
firstCall.disconnect();
StringBuilder cookies = new StringBuilder();
for(HttpCookie cookie:cookieList) {
//if(cookie.SOME_VALIDATION) {
if(cookies.length() > 0) {
cookies.append("; ");
}
cookies.append(cookie.toString());
//}
}
HttpURLConnection secondCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
secondCall.setRequestProperty("Cookie", cookies.toString());
secondCall.connect();
//dosomething
secondCall.disconnect();
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can usesetInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it withHttpURLConnection
, but thanks for the answer anyway.
– John29
Nov 27 '18 at 15:38
There is no Parameter to changeCookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.
– fhofmann
Nov 28 '18 at 7:10
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch fromHttpURLConnection
because I already have a lot of code specific to it.
– John29
Nov 28 '18 at 16:42
add a comment |
In standard oracle implementation the HttpURLConnection
get the default CookieHandler
on the constructor, so this is one possible solution. Create a synchronized singleton factory that create the HttpURLConnections
using a specific manager for each application. Not good idea in my opinion.
Other bad idea is provide your own CookiePolicy
and do the trick on the shouldAccept
method.
Or you can manually control cookies on the app that should not share the CookieHandler
:
HttpURLConnection firstCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
firstCall.connect();
List<HttpCookie> cookieList = HttpCookie.parse(firstCall.getHeaderField("Set-Cookie"));
firstCall.disconnect();
StringBuilder cookies = new StringBuilder();
for(HttpCookie cookie:cookieList) {
//if(cookie.SOME_VALIDATION) {
if(cookies.length() > 0) {
cookies.append("; ");
}
cookies.append(cookie.toString());
//}
}
HttpURLConnection secondCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
secondCall.setRequestProperty("Cookie", cookies.toString());
secondCall.connect();
//dosomething
secondCall.disconnect();
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can usesetInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it withHttpURLConnection
, but thanks for the answer anyway.
– John29
Nov 27 '18 at 15:38
There is no Parameter to changeCookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.
– fhofmann
Nov 28 '18 at 7:10
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch fromHttpURLConnection
because I already have a lot of code specific to it.
– John29
Nov 28 '18 at 16:42
add a comment |
In standard oracle implementation the HttpURLConnection
get the default CookieHandler
on the constructor, so this is one possible solution. Create a synchronized singleton factory that create the HttpURLConnections
using a specific manager for each application. Not good idea in my opinion.
Other bad idea is provide your own CookiePolicy
and do the trick on the shouldAccept
method.
Or you can manually control cookies on the app that should not share the CookieHandler
:
HttpURLConnection firstCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
firstCall.connect();
List<HttpCookie> cookieList = HttpCookie.parse(firstCall.getHeaderField("Set-Cookie"));
firstCall.disconnect();
StringBuilder cookies = new StringBuilder();
for(HttpCookie cookie:cookieList) {
//if(cookie.SOME_VALIDATION) {
if(cookies.length() > 0) {
cookies.append("; ");
}
cookies.append(cookie.toString());
//}
}
HttpURLConnection secondCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
secondCall.setRequestProperty("Cookie", cookies.toString());
secondCall.connect();
//dosomething
secondCall.disconnect();
In standard oracle implementation the HttpURLConnection
get the default CookieHandler
on the constructor, so this is one possible solution. Create a synchronized singleton factory that create the HttpURLConnections
using a specific manager for each application. Not good idea in my opinion.
Other bad idea is provide your own CookiePolicy
and do the trick on the shouldAccept
method.
Or you can manually control cookies on the app that should not share the CookieHandler
:
HttpURLConnection firstCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
firstCall.connect();
List<HttpCookie> cookieList = HttpCookie.parse(firstCall.getHeaderField("Set-Cookie"));
firstCall.disconnect();
StringBuilder cookies = new StringBuilder();
for(HttpCookie cookie:cookieList) {
//if(cookie.SOME_VALIDATION) {
if(cookies.length() > 0) {
cookies.append("; ");
}
cookies.append(cookie.toString());
//}
}
HttpURLConnection secondCall = (HttpURLConnection) new URL("http://www.google.com").openConnection();
secondCall.setRequestProperty("Cookie", cookies.toString());
secondCall.connect();
//dosomething
secondCall.disconnect();
answered Nov 27 '18 at 9:11
fhofmannfhofmann
792515
792515
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can usesetInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it withHttpURLConnection
, but thanks for the answer anyway.
– John29
Nov 27 '18 at 15:38
There is no Parameter to changeCookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.
– fhofmann
Nov 28 '18 at 7:10
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch fromHttpURLConnection
because I already have a lot of code specific to it.
– John29
Nov 28 '18 at 16:42
add a comment |
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can usesetInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it withHttpURLConnection
, but thanks for the answer anyway.
– John29
Nov 27 '18 at 15:38
There is no Parameter to changeCookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.
– fhofmann
Nov 28 '18 at 7:10
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch fromHttpURLConnection
because I already have a lot of code specific to it.
– John29
Nov 28 '18 at 16:42
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can use
setInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it with HttpURLConnection
, but thanks for the answer anyway.– John29
Nov 27 '18 at 15:38
I agree that the first 2 options are bad ideas. Unfortunatelly your code also won't work in my case. The reason is the server I connect to redirects multiple times and some redirects contain cookies. If these cookies are not preserved, the server ends up redirecting in an infinite loop. I can use
setInstanceFollowRedirects(false)
and handle redirects manually, but it will complicate the code even more. I guess there's no good way to do it with HttpURLConnection
, but thanks for the answer anyway.– John29
Nov 27 '18 at 15:38
There is no Parameter to change
CookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.– fhofmann
Nov 28 '18 at 7:10
There is no Parameter to change
CookieHandler
behavior and I imagine that you cannot isolate the web apps in different VMs (or can you? what is your servlet container?). Maybe use apache HTTP client or other API that do not use HTTPUrlConnection internally.– fhofmann
Nov 28 '18 at 7:10
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch from
HttpURLConnection
because I already have a lot of code specific to it.– John29
Nov 28 '18 at 16:42
The servlet container is Tomcat. Using multiple Tomcats is undesirable. I don't want to switch from
HttpURLConnection
because I already have a lot of code specific to it.– John29
Nov 28 '18 at 16:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451280%2fhttpurlconnection-with-a-non-system-wide-cookiehandler%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe this is a solution for you? stackoverflow.com/questions/16305486/…
– chromanoid
Nov 29 '18 at 16:43
@chromanoid No, the solutions from it only help to separate cookies between different threads.
– John29
Nov 29 '18 at 20:07