简体   繁体   English

如何使用维护会话的Java连接到子URL?

[英]How to connect to sub URL using java maintaining the session?

There are two URL, suppose URL1 and URL2. URL有两个,假设URL1和URL2。 URL1 can be used only by provide the proper credentials. URL1仅可通过提供适当的凭据来使用。 I have completed this part by writing the code for simple authentication. 我已经通过编写用于简单身份验证的代码完成了这一部分。 However to connect to URL2, I have to maintain the session from URL1. 但是,要连接到URL2,我必须维护URL1上的会话。 I cannot figure this part out. 我无法弄清楚这部分。 It is showing response code as Forbidden! 它显示响应代码为“禁止!”。

, I have to maintain the session from URL1. ,我必须从URL1维护会话。

It is not likely since on any website, the session is maintained only if the first connexion is maintainted. 因为在任何网站上,只有在维持第一个连接的情况下,才可以维持会话。 HTTP is stateless HTTP是无状态的

When you authenticate on a website, generally, you receive a token in the HTTP response (generally in the headers). 通常,在网站上进行身份验证时,您会在HTTP响应中(通常在标头中)收到令牌。 After being authenticated, for each request, you must re-send that token in your HTTP requests (generally in the headers) otherwise the server considers you as unauthenticated. 在通过身份验证之后,对于每个请求,您都必须在HTTP请求中(通常在标头中)重新发送该令牌,否则服务器会认为您未通过身份验证。

So, to solve your problem, you should analyse what the server give you in headers response when it authenticates you successfully. 因此,要解决您的问题,您应该分析服务器在成功验证您身份时在标头响应中提供的内容。
If you can test your scenario with a browser which owns tools to intercept request/response content, you should easily find the token information sent in the request. 如果您可以使用拥有拦截请求/响应内容工具的浏览器来测试您的方案,则应该轻松地找到请求中发送的令牌信息。

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

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