Hello there! I'm designing an app that needs to authenticate against a
PHP BB system, and then fetch some info from the site. Reading the
samples from the site I've come up with something like this (just the
main methods shown)

public class ForumGrabber {

        private final Properties properties;
        private HttpClient client;
        private ExecutorService executor;
        private MultiThreadedHttpConnectionManager connectionManager;

        public ForumGrabber() {
                properties = new Properties();
                connectionManager = new MultiThreadedHttpConnectionManager();
                this.client = new HttpClient(connectionManager);
                
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
                
this.client.getHostConfiguration().setHost(properties.getProperty(host), 80,
                                "http");
                executor = Executors.newFixedThreadPool(4);
                try {
                        properties.load(ForumGrabber.class.getClassLoader()
                                        
.getResourceAsStream("forum.properties"));
                } catch (Exception e) {

                }
        }
public void doLogin() {
                try {
                String loginURL = properties.getProperty("login.url");
                GetMethod get = new 
GetMethod(properties.getProperty("form.url"));
                client.executeMethod(get);
                get.releaseConnection();
                PostMethod post = new PostMethod(loginURL);
                NameValuePair user = new
NameValuePair(properties.getProperty("form.loginfield"), properties
                                .getProperty("user.name"));
                NameValuePair password = new
NameValuePair(properties.getProperty("form.passwordfield"),
                                properties.getProperty("user.password"));
                post.setRequestBody(new NameValuePair[]{user,password});
                
                        int state = client.executeMethod(post);
                        Cookie[] cookies = client.getState().getCookies();
                        for (Cookie c : cookies) {
                                System.out.println(c.getName());
                                System.out.println(c.getDomain());
                                System.out.println(c.getValue());
                                System.out.println(c.getExpiryDate());
                        }
                        post.releaseConnection();
                } catch (HttpException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

public void processURL(String url) {
                System.out.println("Processing ... " + url);
                GetMethod get = new GetMethod(url);
                
                try {
                        
System.out.println(client.getState().getCookies().length);
                        client.executeMethod(get);
                        String response = get.getResponseBodyAsString();
                        DOMParser parser = new DOMParser();
                        parser.parse(new InputSource(new 
StringReader(response)));
                        org.w3c.dom.NodeList list = 
parser.getDocument().getElementsByTagName("img");
                        List<String> images = new ArrayList<String>();
                        for (int i = 0; i < list.getLength(); i++) {
                                Element e = (Element)list.item(i);
                                String src = e.getAttribute("src");
                                if(src.contains("files")){
                                        images.add(src);
                                }
                        }
                        System.out.println(images.size() + " images found ");
                        processImages(images);
                        String nextDoc = hasNext(response);
                        if (nextDoc != null) {
                                get.releaseConnection();
                                processURL(nextDoc);
                        }
                } catch (HttpException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }  catch (SAXException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ParserException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }finally {
                        get.releaseConnection();
                }
        }

}


The class is invoked like this:

grabber.doLogin();
grabber.processURL("/topics/viewforum.php?t=1000");

The problem is, that it seems that we are not getting authenticated.
Even though after the doLogin execution we get the same cookies we
would get after using firefox to login on the site.

What could be the problem here?


Regards

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to