如何告诉 android 解析重定向到的页面的 XML 响应?

发布于 2024-12-04 11:31:23 字数 434 浏览 0 评论 0原文

我正在尝试解析 Android 应用程序中的 XML 响应。解析技术本身不是问题,但接收 XML 的过程使得用普通方法很难做到这一点。

更详细: 我使用 apache httpclient(在 Android 中)请求一个 xhtml 网站。该网站位于 Java EE 应用程序服务器 (AS) 上。我在请求中提供了两个 GET 参数(用户名、密码)。

该网站位于 AS 上的安全区域,因此首先 AS 将我转发到登录页面。登录页面获取用户名和密码(来自 GET 参数)并自动登录。如果登录凭据有效,我将被重定向到所请求的 XHTML 页面。这是我想用 android SAX 解析器解析的网站。

但是当我尝试这样做时,我能够解析的唯一响应是登录页面,而不是页面。成功登录后我被重定向到。谁能告诉我如何指示 android apache http 客户端在自动登录过程后获取重定向页面的响应(用于稍后解析)?

I'm trying to parse an XML response within an Android app. The technique of parsing itself is not the problem, but the process of receiving the XML makes it difficult to do it the common way.

More in detail:
I request a xhtml website with the apache httpclient (in Android). The website is located on a Java EE Application Server (AS). I give two GET parameters with the request (username, password).

The website is located in a secure area on the AS, so first of all the AS forwards me to the login page. The loginpage takes the username and password (from the GET parameter) and logs me in automatically. If the login credentials are valid I'll get redirected to the requested XHTML page. This is the site I want to parse with the android SAX parser.

But when I try to do this, the only respose I'm able to parse is the login page, not the page. I'm redirected to after successfull login. Can anyone tell me how to instruct the android apache http client to take the response of the redirected page (for later parsing) after the automatical login process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏已如深秋| 2024-12-11 11:31:23

登录的用户存储在 HTTP 会话中,该会话由名为 JSESSIONID 的 cookie 标识。您需要确保在每个后续请求以及重定向时将获得的 cookie 传回。否则,服务器将认为重定向的请求未经授权,并将您再次重定向回登录页面。

可以在 CookieStore 的帮助下管理获得的 cookie 您需要在 中设置您所在的 HttpContext依次需要传递每个 HttpClient#execute() 调用。

HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...

HttpResponse response1 = httpClient.execute(yourMethod1, httpContext);
// ...

HttpResponse response2 = httpClient.execute(yourMethod2, httpContext);
// ...

The logged-in user is stored in the HTTP session which is identified by a cookie with the name JSESSIONID. You need to ensure that you pass the obtained cookie back on every subsequent request, also on the redirects. Otherwise the server will consider the redirected request as unauthorized and redirect you once again back to the login page.

Managing the obtained cookies can be done with help of the CookieStore which you need to set in the HttpContext which you in turn need to pass on every HttpClient#execute() call.

HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...

HttpResponse response1 = httpClient.execute(yourMethod1, httpContext);
// ...

HttpResponse response2 = httpClient.execute(yourMethod2, httpContext);
// ...
染墨丶若流云 2024-12-11 11:31:23

在此博客条目的帮助下找到了我的解决方案:
http://ginger-space.blogspot.com/2007 /04/httpclient-for-form-b​​ased.html

刚刚将代码更新到当前的 Apache 客户端,IT 工作正常。

Found my solution with the help of this blog entry:
http://ginger-space.blogspot.com/2007/04/httpclient-for-form-based.html

Just updated the Code to the current Apache Client andere IT worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文