我如何登录使用的是Android一个JSP网页? [英] How to login to a JSP webpage using Android?

查看:146
本文介绍了我如何登录使用的是Android一个JSP网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要登录到该网站:

This is the site I would need logging in to:

我的目标是一个网站的一些元素结合到一个应用程序,通过获取所需的数据。不幸的是,该网页需要登录。我如何登录到网页从应用程序本身,以便在该网站的用户登录像他将在本机应用程序,并使用会话获取所需的数据?
我虽然实现自定义的CSS,但有这样做的任何其他/更好的办法? JS吧?

My goal is to incorporate some elements of a website into an app, by fetching the required data. Unfortunately, the webpage requires a login. how do i login to the webpage from the app itself so that the user logs in to the site like he would on a native app and use the session to fetch the required data? I though of implementing custom CSS, but is there any other/better way of doing this? JS perhaps?

推荐答案

这是基本相同的方式,你会做,有你试图通过应用程序连接到Gmail。由于presently站,可以使用相同的过程,任何网站。让我们看看如何为Gmail的做到这一点:

This is basically the same way, you would have done it, had you tried to contact gmail via your app. As it presently stands, you can use the same procedure for any website. Let's see how we can do it for Gmail :

现在点​​击基本上发送到服务器的请求,并显示在地层的返回

Now "Clicking" is basically sending a request to a server and displaying the return in formations .

1 /找出所谓的URL该请求(如果它是一个网页,例如见萤火虫)

1/ find out what url to call for that request (if it is a web page, see firebug for example)

2 /找出这些参数,找出如果方法是GET或POST

2/ find out what the parameters are, find out if the method is GET or POST

3 /繁殖亲语法。

4 /一个登录阶段可能意味着一个cookie的使用,让服务器给你,你必须发回之后为每个请求。

4/ a "login" phase probably implies the usage of a cookie, which the server gives you and that you must send back afterward for each request.

一旦你收到的响应,你要检查

Once you receive the response, you'll want to check that

  response.getStatusLine().getStatusCode() < 400

它会告诉你,登录成功。 (2XX的成功,3xx的移动和这样的4xx是错误请求,5XX的服务器端错误;响应的Gmail 302登录建议重定向收件箱)。然后,你会发现,有在包含您想进一步联系该cookie的响应设置Cookie某一特定头这样:

It will tell you that login was successful. (2xx are success, 3xx are moved and such. 4xx are errors in the request, 5xx are server side errors ; Gmail responds 302 to login to suggest redirection to inbox). Then, you'll notice that there is a particular header in the response "Set-Cookie" that contains the cookie you want for further connections so :

  String cookie = response.getFistHeader("Set-Cookie");

然后,您应该能够调用来获取联系人请求:

Then, you should be able to call the request to get the contacts :

HttpGet getContacts = new HttpGet(GMAIL_CONTACTS);
getContacts.setHeader("Cookie", cookie);
response = httpClient.execute(getContacts);
InputStream ins = response.getEntity().getContent();

所以,所有的一切,在code代表的是这样的:

So all in all, the code stands at something like this :

    String GMAIL_CONTACTS = "https://mail.google.com/mail/?shva=1#contacts";
    String GMAIL_LOGIN = "https://mail.google.com";

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(GMAIL_LOGIN);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
    nameValuePairs.add(new BasicNameValuePair("Email", MY_ACC));
    nameValuePairs.add(new BasicNameValuePair("Passwd", MY_PASS));
    nameValuePairs.add(new BasicNameValuePair("signIn", "Sign In"));

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request 
    HttpResponse response = httpClient.execute(httpPost);
    Log.d(TAG, "response stat code " + response.getStatusLine().getStatusCode());

    if (response.getStatusLine().getStatusCode() < 400) {

    String cookie = response.getFirstHeader("Set-Cookie")
    .getValue();
    Log.d(TAG, "cookie: " + cookie);

    // get the contacts page 
    HttpGet getContacts = new HttpGet(GMAIL_CONTACTS);
    getContacts.setHeader("Cookie", cookie);
    response = httpClient.execute(getContacts);

    InputStream ins = response.getEntity().getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(
    ins));

我看不出有任何理由,为什么你不能在你的案件适用相同。这个过程是太相似。

I don't see any reason, why you can't apply the same in your case. The process is all too similar .

这篇关于我如何登录使用的是Android一个JSP网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆