简体   繁体   English

如何使用java.net.URLConnection设置会话属性?

[英]how do you set session attributes with java.net.URLConnection?

how do you set session attributes with java.net.URLConnection or its subclasses? 如何使用java.net.URLConnection或其子类设置会话属性?

In other words, what is the equivalent of HttpSession.setAttribute(String, Object) in Java.net.URLConnection ? 换句话说, Java.net.URLConnectionHttpSession.setAttribute(String, Object)等效于什么? URLConnection does have setRequestProperty(String, String) , but it does not help me as I need similar functionality as session.setAttribute(String, Object) for setting a timestamp. URLConnection确实具有setRequestProperty(String, String) ,但是它对我没有帮助,因为我需要与session.setAttribute(String, Object)类似的功能来设置时间戳。

In other words, how do you express following call with URLConnection? 换句话说,如何使用URLConnection表示后续通话?
HttpSession.setAttribute("timestamp", timestamp);

I cant change the contract with the third party, that is my request must have an timestamp attribute with a java.util.Date value. 我无法更改与第三方的合同,也就是说,我的请求必须具有带有java.util.Date值的timestamp属性。

Any help appreciated. 任何帮助表示赞赏。

You can't. 你不能 The session is kept, privately, at server-side, and is not available from the client. 该会话是在服务器端私下保存的,客户端无法使用。 It's not part of the HTTP protocol at all, but is just something that nearly all server-side webapp APIs offer to allow keeping state associated to one particular user of the application. 它根本不是HTTP协议的一部分,而几乎是所有服务器端Webapp API都提供的功能,以允许将状态与应用程序的一个特定用户相关联。

Basically, what you're asking is the equivalent of "what can I put in my email to store some text in the C:\\session.txt file on the hard drive of the recipient?": 基本上,您要问的是“我可以在电子邮件中放入哪些内容以在收件人硬盘上的C:\\session.txt文件中存储一些文本?”:

  • there is perhaps no C:\\session.txt file 可能没有C:\\session.txt文件
  • you can't access the hard drive of a recipient by writing something in an email. 您无法通过在电子邮件中写一些东西来访问收件人的硬盘。 It would constitute a big security problem 这将构成一个很大的安全问题

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

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