带有“ Cookie”的HttpURLConnection addRequestProperty导致逗号分隔的Cookie字符串? [英] HttpURLConnection addRequestProperty with "Cookie" resulting in comma separated Cookie string?

查看:295
本文介绍了带有“ Cookie”的HttpURLConnection addRequestProperty导致逗号分隔的Cookie字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从Java代码模拟浏览器的行为。



在将请求发布到服务器之前,我必须在请求上设置两个cookie。



我这样做:

  HttpURLConnection conn = ... 
...
conn.addRequestProperty( Cookie, IDS_SSO_ID = + onething);
conn.addRequestProperty( Cookie, JSESSIONID = + otherthing)));
...
conn.close();

在服务器日志上,我看到 IDS_SSO_ID cookie被检索为 onething,JSESSIONID,这会导致错误。



请注意,我无权访问服务器或服务器的源代码,我只有日志。



如何使用HttpURLConnection设置cookie?






因此,我创建了一个小示例;如果我使用 addRequestProperty,则会发送不正确的Cookie标头:

  URL url = new URL( https:// en0hphl04qcwvf .x.pipedream.net /); 
HttpURLConnection conn =(HttpURLConnection)url.openConnection();

conn.addRequestProperty( Cookie, JSESSIONID = akarmi123);
conn.addRequestProperty( Cookie, IDS_SSO_ID = netudd321);

byte [] bytes = StreamUtils.copyToByteArray(conn.getInputStream());
System.out.println( response: +新的String(bytes));
conn.disconnect();

cookie标头值为: JSESSIONID = akarmi123,IDS_SSO_ID = netudd321

如果我使用'setRequestProperty'并手动构造cookie头,则会发送正确的cookie头:

  conn =(HttpURLConnection)url.openConnection(); 

conn.setRequestProperty( Cookie, JSESSIONID = akarmi123; IDS_SSO_ID = netudd321);

字节= StreamUtils.copyToByteArray(conn.getInputStream());
System.out.println( response: +新的String(bytes));
conn.disconnect();

cookie标头值为: JSESSIONID = akarmi123; IDS_SSO_ID = netudd321



奇怪的是,网络中的大量资源(以及SO中的资源)推荐了我的第一种方法-多次调用在addRequestProperty(...)上:



如何使用Java在Http Get方法中设置Cookie



https://www.codota.com/code/java/methods/java.net.URLConnection/addRequestProperty



https://www.programcreek.com/java-api-examples/?class=java.net.HttpURLConnection&method=addRequestProperty



http:// w ww.massapi.com/method/ad/addRequestProperty-2.html



但似乎他们错了...

解决方案

一种可能性是通过执行以下操作来强制cookie字符串的正确连接:



< pre class = lang-java prettyprint-override> conn.addRequestProperty( Cookie, JSESSIONID = + otherthing +; IDS_SSO_ID = + onething);

您所描述的行为在我看来是意外行为。


I have to emulate browser behaviour from Java code.

I have to set two cookies on a request before posting it to the server.

I do this:

HttpURLConnection conn = ...
...
conn.addRequestProperty("Cookie", "IDS_SSO_ID=" + "onething");
conn.addRequestProperty("Cookie", "JSESSIONID=" + "otherthing"));
...
conn.close();

On the server logs I see that the 'IDS_SSO_ID' cookie is retrieved as "onething, JSESSIONID", which causes an error.

Note that I have no access to the server nor the server's source code, I only have the logs.

How should I set cookies using HttpURLConnection?


So, I've created a small demonstration; If I use 'addRequestProperty' then an incorrect cookie header is sent:

URL url = new URL("https://en0hphl04qcwvf.x.pipedream.net/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.addRequestProperty("Cookie", "JSESSIONID=akarmi123");
conn.addRequestProperty("Cookie", "IDS_SSO_ID=netudd321");

byte[] bytes = StreamUtils.copyToByteArray(conn.getInputStream());
System.out.println("response: " + new String(bytes));
conn.disconnect();

cookie header value is: JSESSIONID=akarmi123, IDS_SSO_ID=netudd321

If I use 'setRequestProperty' and manually construct the cookie header, then a correct cookie header is sent:

conn = (HttpURLConnection) url.openConnection();

conn.setRequestProperty("Cookie", "JSESSIONID=akarmi123; IDS_SSO_ID=netudd321");

bytes = StreamUtils.copyToByteArray(conn.getInputStream());
System.out.println("response: " + new String(bytes));
conn.disconnect();

cookie header value is: JSESSIONID=akarmi123; IDS_SSO_ID=netudd321

The strange thing is, that a lot of resources in the web (and here in SO too) recommends my first approach -- multiple calls on addRequestProperty(...):

How to set Cookies at Http Get method using Java

https://www.codota.com/code/java/methods/java.net.URLConnection/addRequestProperty

https://www.programcreek.com/java-api-examples/?class=java.net.HttpURLConnection&method=addRequestProperty

http://www.massapi.com/method/ad/addRequestProperty-2.html

But it seems that they are wrong...

解决方案

A possibility would be to enforce the right concatenation of the cookie strings by doing the following:

conn.addRequestProperty("Cookie", "JSESSIONID=" + "otherthing" + ";IDS_SSO_ID=" + "onething");

The behavior described by you looks to me as an unintended behavior.

这篇关于带有“ Cookie”的HttpURLConnection addRequestProperty导致逗号分隔的Cookie字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆