HttpPost到asp服务器上(HTML形式)
使用 HttpURLConnection 并发布到服务器,我获得了带有位置标头的重定向页面。状态代码 302,因此我必须使用此信息进行重定向。 (这里一切似乎都很好..至少对我来说)
08-03 09:45:11.662: INFO/System.out(1664): <html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://website/mobile/(X(1)S(sf3dah5520rblujg4z2n0n55))/Login.aspx?ReturnUrl=%2fsyspower3%2fmobile%2fDefault.aspx&AspxAutoDetectCookieSupport=1">here</a>.</h2>
</body></html>
08-03 09:45:11.662: INFO/System.out(1664): Status code - 302
08-03 09:45:11.672: INFO/System.out(1664): location : [http://website/mobile/(X(1)S(sf3dah5520rblujg4z2n0n55))/Login.aspx?ReturnUrl=%2fsyspower3%2fmobile%2fDefault.aspx&AspxAutoDetectCookieSupport=1]
08-03 09:45:11.672: INFO/System.out(1664): x-powered-by : [ASP.NET]
08-03 09:45:11.672: INFO/System.out(1664): content-type : [text/html; charset=utf-8]
08-03 09:45:11.672: INFO/System.out(1664): content-length : [282]
08-03 09:45:11.672: INFO/System.out(1664): date : [Wed, 03 Aug 2011 09:45:00 GMT]
08-03 09:45:11.672: INFO/System.out(1664): server : [Microsoft-IIS/7.5]
08-03 09:45:11.672: INFO/System.out(1664): location found: http://website/mobile/(X(1)S(sf3dah5520rblujg4z2n0n55))/Login.aspx?ReturnUrl=%2fsyspower3%2fmobile%2fDefault.aspx&AspxAutoDetectCookieSupport=1
在此之后,如果我在普通浏览器中使用此链接,我可以在不登录的情况下进入服务器,所以实际上这个链接是我应该用来发表帖子的链接并获取登录后服务器内部的信息。 (如果我在浏览器中使用它,它实际上会这样做)
问题: 如果我对已经使用新位置的服务器执行新的POST,我会再次进入登录屏幕。 我应该为新请求使用 cookie 还是我的问题是什么?
Using HttpURLConnection and doing post to the server I obtain the redirect page with the location header. Status code 302, so I must use this information to proceed to the redirect. (Everything seems to be ok here.. at least for me)
08-03 09:45:11.662: INFO/System.out(1664): <html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://website/mobile/(X(1)S(sf3dah5520rblujg4z2n0n55))/Login.aspx?ReturnUrl=%2fsyspower3%2fmobile%2fDefault.aspx&AspxAutoDetectCookieSupport=1">here</a>.</h2>
</body></html>
08-03 09:45:11.662: INFO/System.out(1664): Status code - 302
08-03 09:45:11.672: INFO/System.out(1664): location : [http://website/mobile/(X(1)S(sf3dah5520rblujg4z2n0n55))/Login.aspx?ReturnUrl=%2fsyspower3%2fmobile%2fDefault.aspx&AspxAutoDetectCookieSupport=1]
08-03 09:45:11.672: INFO/System.out(1664): x-powered-by : [ASP.NET]
08-03 09:45:11.672: INFO/System.out(1664): content-type : [text/html; charset=utf-8]
08-03 09:45:11.672: INFO/System.out(1664): content-length : [282]
08-03 09:45:11.672: INFO/System.out(1664): date : [Wed, 03 Aug 2011 09:45:00 GMT]
08-03 09:45:11.672: INFO/System.out(1664): server : [Microsoft-IIS/7.5]
08-03 09:45:11.672: INFO/System.out(1664): location found: http://website/mobile/(X(1)S(sf3dah5520rblujg4z2n0n55))/Login.aspx?ReturnUrl=%2fsyspower3%2fmobile%2fDefault.aspx&AspxAutoDetectCookieSupport=1
After this, if i use this link in the normal browser, i can go into the server without logon, so practically this link is the one i should use to make a post to and obtain the information that is inside the server after the logon. (And it actually does it if I use it in browser)
Problem:
If i do the new POST to the server already with the new location, i get to the logon screen again.
Should I be using cookies for the new request or what is my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,cookies应该可以解决你的问题。
当您使用浏览器时,您第一次访问该站点时会收到 Set-Cookie 响应标头。然后,当您继续时,浏览器会发送标头 Cookie,其中包含所有 cookie。
其中一个 cookie 包含 HTTP 会话 ID。例如,基于 java 的服务器使用 cookie
jsessionid
。当您执行第二个、第三个等请求时,服务器使用此 cookie 来识别您的会话。如果该字段不存在,服务器每次都会创建新会话。该会话是新的,因此未经过身份验证,因此您将被重定向到登录页面。Yes, cookies should solve your problem.
When you are working with browser you get Set-Cookie response header when you go to the site first time. Then when you continue the browser sends header Cookie with all cookies in it.
One of cookie contains HTTP session ID. For example java based servers use cookie
jsessionid
. This cookie is used by server to identify your session when you perform second, third etc request. If this field is absent server creates new session every time. The session is new, therefore is not authenticated, so you are redirected to login page.