从 Java 管理 ASP.NET 站点的会话 (Apache HttpClient)
我想从只能从会话内访问的 ASP.NET 站点获取网页。我正在使用 Apache HttpClient。我首先打开网站的主页,然后搜索“目标”页面的链接,然后启动“目标”页面的 GET 请求。问题是,当我收到第二个 GET 请求的响应时,我总是得到相同的(第一个)页面。如果我使用 Firefox 或 Google Chrome 打开该网站,我会看到“目标”页面。
从服务器的第一个响应中,我得到以下标头:
HTTP/1.1 200 OK
Date: Sun, 12 Dec 2010 19:03:56 GMT
Server: Microsoft-IIS/6.0
Platform: Mobitel Pla.NET
Node: 4
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=0vpgd055cifko3mnw4nkuimz; path=/
Cache-Control: no-cache, must-revalidate
Content-Type: text/html; charset=utf-8
Content-Length: 7032
我使用 WireShark 检查了流量,所有标头看起来都正常。我在第二个 GET 请求时将正确的 cookie 发送回服务器。
我正在使用 Apache HttpClient。我只有一个 DefaultHttpClient 实例,我将其重用于第二个请求。我有 BROWSER_COMPATIBILITY Cookie 政策。
有什么想法吗?
I want to fetch a web page from a ASP.NET site that is only accessible from within a session. I'm using Apache HttpClient. I first open the main page of the site, then I search for the link to the "goal" page, and then I fire up a GET request for the "goal" page. The problem is that when I get the response for the second GET request, I always get the same (first) page. If I open the site with Firefox or Google Chrome I get the "goal" page.
From the first response from the server I get the following headers:
HTTP/1.1 200 OK
Date: Sun, 12 Dec 2010 19:03:56 GMT
Server: Microsoft-IIS/6.0
Platform: Mobitel Pla.NET
Node: 4
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=0vpgd055cifko3mnw4nkuimz; path=/
Cache-Control: no-cache, must-revalidate
Content-Type: text/html; charset=utf-8
Content-Length: 7032
I inspected the traffic with WireShark and all headers look OK. I send the correct cookie back to the server on the second GET request.
I'm using Apache HttpClient. I have only one instance of DefaultHttpClient and I reuse that for the second request. I have BROWSER_COMPATIBILITY Cookie Policy.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在所有进一步的请求中从客户端发回此标头(发回您收到的 cookie):
这应该可以解决问题
You need send back this header from the client (send back the cookie you received) in all your further requests:
That should do the trick
我发现了我的愚蠢错误。
错误在于我向链接发送了第二个 GET 请求,而没有替换 & 字符代码。
例如:
这是错误的,因为我发送
&
而不是&
正确的方法是:
I found my stupid mistake.
The mistake was that I was sending the second GET request to a link, without replacing the ampersand character codes.
Ex:
That is wrong, because I send
&
instead of&
The RIGHT way to do it is: