简体   繁体   English

从Java(Apache HttpClient)管理与ASP.NET站点的会话

[英]Managing a session with ASP.NET site from Java (Apache HttpClient)

I want to fetch a web page from a ASP.NET site that is only accessible from within a session. 我想从ASP.NET站点中获取一个网页,该站点只能从会话中访问。 I'm using Apache HttpClient. 我正在使用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. 我首先打开网站的主页,然后搜索到“目标”页面的链接,然后为“目标”页面启动GET请求。 The problem is that when I get the response for the second GET request, I always get the same (first) page. 问题是,当我收到第二个GET请求的响应时,总是得到相同的(第一)页面。 If I open the site with Firefox or Google Chrome I get the "goal" page. 如果我使用Firefox或Google Chrome打开网站,则会看到“目标”页面。

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. 我用WireShark检查了流量,所有标题看起来都不错。 I send the correct cookie back to the server on the second GET request. 我在第二个GET请求上将正确的cookie发送回服务器。

I'm using Apache HttpClient. 我正在使用Apache HttpClient。 I have only one instance of DefaultHttpClient and I reuse that for the second request. 我只有一个DefaultHttpClient实例,并将其重用于第二个请求。 I have BROWSER_COMPATIBILITY Cookie Policy. 我有BROWSER_COMPATIBILITY Cookie政策。

Any ideas? 有任何想法吗?

You need send back this header from the client (send back the cookie you received) in all your further requests: 您需要在所有其他请求中从客户端发回此标头(发回收到的cookie):

Cookie: ASP.NET_SessionId=0vpgd055cifko3mnw4nkuimz; // and all other cookies

That should do the trick 这应该够了吧

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. 错误是我将第二个GET请求发送到链接,而没有替换“&”字符代码。

Ex: 例如:

/(0vpgd055cifko3mnw4nkuimz)/Mp.aspx?ni=1482&pi=72&_72_url=925b9749-b7c7-4615-9f1a-9b613c344c82

That is wrong, because I send & 这是错误的,因为我发送& instead of & 代替&

The RIGHT way to do it is: 正确的方法是:

/(0vpgd055cifko3mnw4nkuimz)/Mp.aspx?ni=1482&pi=72&_72_url=925b9749-b7c7-4615-9f1a-9b613c344c82

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

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