Using Cookies with the Parser

Problem: (by ShanSivakolundhu)
In order to access a particular site I neet to have a cookie set. Is there any way I can set the cookie before I create a parser object ? Just like ...

URLConnection.("Cookie", cookieValue);
URLConnection.connect();

Solution: (by BobLewis)
In order to send cookies in your Http requests, all you need to do is set the Cookie HTTP Header in the URL Connection.

Create the URL and open the connection, but before passing the connection to the parser, set the "Cookie" request property:

import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.Cookie;

import org.htmlparser.Parser;
import org.htmlparser.util.NodeIterator;

/**
 * Demonstrate cookie usage with the HTML Parser.
 */
public class CookieDemo
{
    /**
     * The cookies.
     * You'll need to get these from your browser's cookie jar or somewhere.
     * Only the cookies that apply to the URL you are using and haven't expired
     * are supposed to be passed in the request.
     * This is only part of a real cookie, much longer than shown.
     */
    public static Cookie[] cookies =
    {
        new Cookie ("user", "%2536%2535%2538%2531%2539%2530%253a etc."),
    };

    /**
     * Generate a HTTP cookie header value string from an array of cookies.
     * <pre>
     *   The syntax for the header is:
     *
     *    cookie          =       "Cookie:" cookie-version
     *                            1*((";" | ",") cookie-value)
     *    cookie-value    =       NAME "=" VALUE [";" path] [";" domain]
     *    cookie-version  =       "$Version" "=" value
     *    NAME            =       attr
     *    VALUE           =       value
     *    path            =       "$Path" "=" value
     *    domain          =       "$Domain" "=" value
     *
     * </pre>
     * @param cookies The cookies which should be set in the header value.
     * @return A string containing the HTTP Cookie Header value.
     * @see <a href="http://www.ietf.org/rfc/rfc2109.txt">RFC 2109</a>
     */
    public static String generateCookieHeader (Cookie[] cookies)
    {
        int version;
        boolean quote;
        StringBuffer ret;

        ret = new StringBuffer ();

        version = 0;
        for (int i = 0; i < cookies.length; i++)
            version = Math.max (version, cookies[i].getVersion ());
        if (0 != version)
        {
            ret.append ("$Version=\"");
            ret.append (version);
            ret.append ("\"");
        }
        for (int i = 0; i < cookies.length; i++)
        {
            if (0 != ret.length ())
                ret.append ("; ");
            ret.append (cookies[i].getName ());
            ret.append ("=");
            if (0 != version)
                ret.append ("\"");
            ret.append (cookies[i].getValue ());
            if (0 != version)
                ret.append ("\"");
            if (0 != version)
            {
                if ((null != cookies[i].getPath ())
                    && (0 != cookies[i].getPath ().length ()))
                {
                    ret.append ("; $Path=\"");
                    ret.append (cookies[i].getPath ());
                    ret.append ("\"");
                }
                if ((null != cookies[i].getDomain ())
                    && (0 != cookies[i].getDomain ().length ()))
                {
                    ret.append ("; $Domain=\"");
                    ret.append (cookies[i].getDomain ());
                    ret.append ("\"");
                }
            }
        }

        return (ret.toString ());
    }

    public static void main (String[] args) throws Exception
    {
        Parser parser;
        URL url;
        URLConnection connection;

        parser = new Parser ();
        url = new URL ("http://slashdot.org");
        connection = url.openConnection ();
        connection.setRequestProperty ("Cookie", generateCookieHeader (cookies));
        parser.setConnection (connection);
        for (NodeIterator iterator = parser.elements (); iterator.hasMoreNodes (); )
            System.out.println (iterator.nextNode ());
    }
}




Last edited on Monday, January 26, 2004 7:26:47 pm.