Android Jsoup登录以获得响应

时间:2012-07-07 03:51:04

标签: android login jsoup

我正在尝试为移动网站的登录失败或成功做出回应。这是我的代码的一部分。

修改


public String login(String User, String Pass) throws Exception{
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();


    HttpPost httppost = new HttpPost(AmwayURL);
    String result = "nothing";
    try {
        // Add user name and password
        String username = User;
        String password = Pass;

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("userid", username));
        nameValuePairs.add(new BasicNameValuePair("userpswd", password));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        CookieStore cookieStore = new BasicCookieStore();
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        HttpResponse response = httpclient.execute(httppost, localContext);

        result = inputStreamToString(response.getEntity().getContent()).toString();

        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        List<Cookie> cookies = cookieStore.getCookies();
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("Local cookie: " + cookies.get(i));
        }

        // Consume response content
        //EntityUtils.consume(entity);

        System.out.println("----------------------------------------");



    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

我确实得到了一个回复,如果我检查它,我可以看到我确实执行了登录,但我的问题是如何使用编程方式来确定登录是否成功?

我无法使用重定向代码,因为该网站停留在同一页面上。

1 个答案:

答案 0 :(得分:0)

将您的回复传递给String,然后只需按照官方文档解析即可!

http://jsoup.org/cookbook/input/parse-document-from-string

如果您需要解析特定元素,您可以随时执行:

doc.select(your CSS query here searching a tag, element, class, id or even some atribute value)