File: UsingCookiesWithParser

package info (click to toggle)
libhtmlparser-java 1.6.20060610.dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 6,736 kB
  • ctags: 3,706
  • sloc: java: 35,078; sh: 2,502; xml: 468; makefile: 27
file content (229 lines) | stat: -rw-r--r-- 9,492 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- $Id: head.tmpl,v 1.5 2002/12/15 01:30:47 carstenklapp Exp $ -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="robots" content="index,follow" />
<meta name="keywords" content="Using Cookies With Parser, PhpWiki" />
<meta name="description" content="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 ..." />
<meta name="language" content="" />
<meta name="document-type" content="Public" />
<meta name="document-rating" content="General" />
<meta name="generator" content="phpWiki" />
<meta name="PHPWIKI_VERSION" content="1.3.4" />

<link rel="shortcut icon" href="/wiki/themes/default/images/favicon.ico" />
<link rel="home" title="HomePage" href="HomePage" />
<link rel="help" title="HowToUseWiki" href="HowToUseWiki" />
<link rel="copyright" title="GNU General Public License" href="http://www.gnu.org/copyleft/gpl.html#SEC1" />
<link rel="author" title="The PhpWiki Programming Team" href="http://phpwiki.sourceforge.net/phpwiki/ThePhpWikiProgrammingTeam" />
<link rel="search" title="FindPage" href="FindPage" />
<link rel="alternate" title="View Source: UsingCookiesWithParser" href="UsingCookiesWithParser?action=viewsource&amp;version=8" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="RecentChanges?format=rss" />

<link rel="bookmark" title="SandBox" href="SandBox" />
<link rel="bookmark" title="WikiWikiWeb" href="WikiWikiWeb" />



<link rel="stylesheet" title="MacOSX" type="text/css" charset="iso-8859-1" href="/wiki/themes/MacOSX/MacOSX.css" /><link rel="alternate stylesheet" title="Printer" type="text/css" charset="iso-8859-1" href="/wiki/themes/default/phpwiki-printer.css" media="print, screen" /><link rel="alternate stylesheet" title="Modern" type="text/css" charset="iso-8859-1" href="/wiki/themes/default/phpwiki-modern.css" /><style type="text/css">
<!--
body {background-image: url(/wiki/themes/MacOSX/images/bgpaper8.png);}
-->
</style>
<title>PhpWiki - Using Cookies With Parser</title>
</head>
<!-- End head -->
<!-- Begin body -->
<!-- $Id: body.tmpl,v 1.30 2002/09/02 14:36:58 rurban Exp $ -->
<body>
<!-- Begin top -->
<!-- $Id: top.tmpl,v 1.20 2002/12/15 01:30:47 carstenklapp Exp $ -->

<!-- End top -->
<!-- Begin browse -->
<!-- $Id: browse.tmpl,v 1.22 2002/02/19 23:00:26 carstenklapp Exp $ -->


<div class="wikitext"><p><b>Using Cookies with the Parser</b></p>
<p><b>Problem:</b> (by ShanSivakolundhu)<br />
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 ...</p>
<pre>
URLConnection.("Cookie", cookieValue);
URLConnection.connect();</pre>
<p><b>Solution:</b> (by BobLewis)<br />
In order to send cookies in your Http requests, all
you need to do is set the Cookie HTTP Header in the
URL Connection.</p>
<p>Create the URL and open the connection, but before passing
the connection to the parser, set the "Cookie" request property:</p>
<pre>
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.
     * &lt;pre&gt;
     *   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
     *
     * &lt;/pre&gt;
     * @param cookies The cookies which should be set in the header value.
     * @return A string containing the HTTP Cookie Header value.
     * @see &lt;a href="http://www.ietf.org/rfc/rfc2109.txt"&gt;RFC 2109&lt;/a&gt;
     */
    public static String generateCookieHeader (Cookie[] cookies)
    {
        int version;
        boolean quote;
        StringBuffer ret;

        ret = new StringBuffer ();

        version = 0;
        for (int i = 0; i &lt; 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 &lt; 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 ())
                    &amp;&amp; (0 != cookies[i].getPath ().length ()))
                {
                    ret.append ("; $Path=\"");
                    ret.append (cookies[i].getPath ());
                    ret.append ("\"");
                }
                if ((null != cookies[i].getDomain ())
                    &amp;&amp; (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 ());
    }
}</pre>
</div>


<!-- End browse -->
<!-- Begin bottom -->
<!-- $Id: bottom.tmpl,v 1.3 2002/09/15 20:21:16 rurban Exp $ -->
<!-- Add your Disclaimer here -->
<!-- Begin debug -->
<!-- $Id: debug.tmpl,v 1.9 2002/09/17 02:10:33 dairiki Exp $ -->
<table width="%100" border="0" cellpadding="0" cellspacing="0">
<tr><td>

</td><td>
<span class="debug">Page Execution took 0.301 seconds</span>
</td></tr></table>
<!-- This keeps the valid XHTML! icons from "hanging off the bottom of the scree" -->
<br style="clear: both;" />
<!-- End debug -->
<!-- End bottom -->
</body>
<!-- End body -->
<!-- phpwiki source:
$Id: prepend.php,v 1.13 2002/09/18 19:23:25 dairiki Exp $
$Id: ErrorManager.php,v 1.16 2002/09/14 22:23:36 dairiki Exp $
$Id: HtmlElement.php,v 1.27 2002/10/31 03:28:30 carstenklapp Exp $
$Id: XmlElement.php,v 1.17 2002/08/17 15:52:51 rurban Exp $
$Id: WikiCallback.php,v 1.2 2001/11/21 20:01:52 dairiki Exp $
$Id: index.php,v 1.99 2002/12/31 01:13:14 wainstead Exp $
$Id: main.php,v 1.90 2002/11/19 07:07:37 carstenklapp Exp $
$Id: config.php,v 1.68 2002/11/14 22:28:03 carstenklapp Exp $
$Id: FileFinder.php,v 1.11 2002/09/18 18:34:13 dairiki Exp $
$Id: Request.php,v 1.24 2002/12/14 16:21:46 dairiki Exp $
$Id: WikiUser.php,v 1.29 2002/11/19 07:07:38 carstenklapp Exp $
$Id: WikiDB.php,v 1.17 2002/09/15 03:56:22 dairiki Exp $
$Id: SQL.php,v 1.2 2001/09/19 03:24:36 wainstead Exp $
$Id: mysql.php,v 1.3 2001/12/08 16:02:35 dairiki Exp $
$Id: PearDB.php,v 1.28 2002/09/12 11:45:33 rurban Exp $
$Id: backend.php,v 1.3 2002/01/10 23:32:04 carstenklapp Exp $
$Id: DB.php,v 1.2 2002/09/12 11:45:33 rurban Exp $
From Pear CVS: Id: DB.php,v 1.13 2002/07/02 15:19:49 cox Exp
$Id: PEAR.php,v 1.1 2002/01/28 04:01:56 dairiki Exp $
From Pear CVS: Id: PEAR.php,v 1.29 2001/12/15 15:01:35 mj Exp
$Id: mysql.php,v 1.2 2002/09/12 11:45:33 rurban Exp $
From Pear CVS: Id: mysql.php,v 1.5 2002/06/19 00:41:06 cox Exp
$Id: common.php,v 1.2 2002/09/12 11:45:33 rurban Exp $
From Pear CVS: Id: common.php,v 1.8 2002/06/12 15:03:16 fab Exp
$Id: themeinfo.php,v 1.46 2002/03/08 20:31:14 carstenklapp Exp $
$Id: Theme.php,v 1.58 2002/10/12 08:55:03 carstenklapp Exp $
$Id: display.php,v 1.38 2002/09/15 20:17:58 rurban Exp $
$Id: Template.php,v 1.46 2002/09/15 15:05:47 rurban Exp $
$Id: WikiPlugin.php,v 1.27 2002/11/04 03:15:59 carstenklapp Exp $
$Id: BlockParser.php,v 1.29 2002/11/25 22:25:49 dairiki Exp $
$Id: InlineParser.php,v 1.19 2002/11/25 22:51:37 dairiki Exp $
$Id: interwiki.php,v 1.23 2002/10/06 16:45:10 dairiki Exp $
$Id: PageType.php,v 1.13 2002/09/04 20:39:47 dairiki Exp $
-->
</html>