HttpClient登录,搜索并获取XML内容 [英] HttpClient login, search and get the XML content

查看:88
本文介绍了HttpClient登录,搜索并获取XML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用HttpClient登录到站点,登录后我想搜索一些东西并检索搜索结果的内容.

I want to login to a site using HttpClient and after logging in I want to search for something and retrieve the contents of the search result.

/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 */
public class TestHttpClient {

    public static void main(String[] args) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet("http://projecteuler.net/");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("http://projecteuler.net/index.php?section=login");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("IDToken1", "username"));
        nvps.add(new BasicNameValuePair("IDToken2", "password"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);

        System.out.println("Response "+response.toString());
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {

            InputStream is = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str ="";
            while ((str = br.readLine()) != null){
                System.out.println(""+str);
            }
        }

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
        httpclient.getConnectionManager().shutdown();        
    }
}

当我打印HttpEntity的输出时,它正在打印登录页面的内容.使用HttpClient登录后如何获取页面内容?

when I print the output from HttpEntity it's printing the login page contents. How do I get the contents of the page after I login using HttpClient?

推荐答案

帖子应模仿表单提交.无需先获取登录页面. 如果我看一下 http://projecteuler.net ,似乎该表单已发布到index.php,因此我会尝试更改帖子的网址:

The post should mimick the form submit. No need to get the login page first. If I take a look at http://projecteuler.net, it seems the form is posted to index.php, so I'd try changing the post url:

HttpPost httpost = new HttpPost("http://projecteuler.net/index.php");

使用类似Fire bug之类的东西来查看浏览器中到底发生了什么.也许您应该在登录后遵循重定向(HttpClient支持此操作). 似乎还有一个名为"login"的参数,其值是"Login".

Use something like Fire bug to see what is exactly happening in the browser. Maybe you should follow a redirect after logging in (HttpClient supports this). There also seems to be a parameter called "login"with value "Login" that is being posted.

这篇关于HttpClient登录,搜索并获取XML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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