Thursday, March 24, 2011

A Little Known Library: Prowser

In the development of my next Android app, a mobile client for a web forum, I was looking for a library to make HTTP requests. The most agreed upon library to use was Apache's HTTPClient. Which this can get the job done, it was too much for me and sought something easier. Then I came across Prowser. The description drew me in immediately.

The Prowser library enables applications to easily surf the web via an API that simulates the way a human user would operate a tabbed GUI browser.

That's something I can get behind. Now the library is complete with some good documentation and Javadocs but like always, when I eventually had a problem, I searched around and found virtually nothing on it. I was compelled to write something about it, talk about it a little bit. It's been so useful for me and I must share it.

Like the discription says, "Prowser simulates a way a human user would operate a tabbed GUI browser." For me, I imagined Prowser like my browser and I just copy-paste the source from the page and put it into an interface I want. I let the browser handle everything else, most importantly (for me): cookies. I was having a hard time capturing and sending cookies to manage a user session on the forum but Prowser made it easy.

I hope these quick guides can help you get starting using Prowser:

Loading a page (a GET request):

    public class Browser {
       Tab tab = new Prowser().createTab();    //A new browser tab
        public static String browser(String url) {
            Response response = tab.go(url);     //runs the url
            String result = response.getPageSource();  //Gets HTML response
            return result;

Prowser works in Tabbed instances like the browser you're using now. First make a new tab. Next we make a Response varible and have it run tab.go(String url), which runs the url given. Like typing in in your address bar and hitting enter. Next we get the source from the Response as a string with .getPageSource(). With that I can parse it and format it how I want for the application.

Filling in a form (a POST request):

    public class Browser {
        Tab tab = new Prowser().createTab();    //A new browser tab
        public static String login(String username, String password)  {
        //New request to do POST command
            Request req = new Request(";   
            req.addParameter("b", username);
            req.addParameter("p", password);
            req.addParameter("r", "");
            Response result = tab.go(req);
            return result;

The forum I'm building this app for uses form based authentication. Submitting the information uses the HTTP POST request which Prowser supports. First we get a new Prowser tab. Next we build a Request. We won't simply use tab.go(String url). The Request contains the URL, HTTP Method (in this case, POST), and then we add our parameters. I needed three. We use Request.addParameter (String name, String value). You will want to check the website you're POSTing to and check what the form name is. I used Firebug to get that information. After your values are added we run the Tab for a Response but instead of a url in go() we send the request: tab.go(Request).
Pro-tip: I suggest keeping a tab in it's own class and have other classes call to it instead of creating a new one each time. Especially with any type of session management in my case, a forum.  This keeps the instance alive and able to hold the cookies and other delicious stuff.

No comments:

Post a Comment