htmlParser在设置cookies时存在bug,对于相同的domain,不能设置多个cookies.下面时偶提交的bug内容及解决办法.偶英语很烂--大家别笑话.:P
There can't set many cookies to a domain,for example:
public
void
testSetCookies()
throws
Exception
{
String urlString
= " http://sourceforge.net/projects/htmlparser " ;
Parser parser = this .buildParser(urlString);
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
}
private Parser buildParser(String urlString) throws
Exception
{
ConnectionManager manager =
Parser.getConnectionManager ();
Cookie cookie = new Cookie ( " name1 " , " value1 " );
manager.setCookie (cookie, " sourceforge.net " );
cookie = new Cookie ( " name2 " , " value2 " );
manager.setCookie (cookie, " sourceforge.net " );
return new Parser(urlString);
}
{
String urlString
= " http://sourceforge.net/projects/htmlparser " ;
Parser parser = this .buildParser(urlString);
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
}
private Parser buildParser(String urlString) throws
Exception
{
ConnectionManager manager =
Parser.getConnectionManager ();
Cookie cookie = new Cookie ( " name1 " , " value1 " );
manager.setCookie (cookie, " sourceforge.net " );
cookie = new Cookie ( " name2 " , " value2 " );
manager.setCookie (cookie, " sourceforge.net " );
return new Parser(urlString);
}
only the first cookie was set to the
domain "sourceforge.net".
This bug comes with the
method:ConnectionManager.setCookie (Cookie cookie,
String domain),there can't add a new cookie to a exist
domain.
It is ok if repleace the method with under codes :
public
void
setCookie (Cookie cookie, String domain)
{
String path;
Vector cookies;
Cookie probe;
if ( null != cookie.getDomain ())
domain = cookie.getDomain ();
path = cookie.getPath ();
if ( null == mCookieJar)
mCookieJar = new Hashtable (); // turn on
cookie processing
cookies = (Vector)mCookieJar.get (domain);
if ( null != cookies)
{
boolean isNewCookie = true ;
for ( int j = 0 ; j < cookies.size (); j ++ )
{
probe = (Cookie)cookies.elementAt (j);
if (probe.getName ().equalsIgnoreCase
(cookie.getName ()))
{
if (isNewCookie == true ) isNewCookie =
false ;
// we keep paths sorted most specific to
least
if (probe.getPath ().equals (path))
{
cookies.setElementAt (cookie, j); //
replace
break ;
}
else if (path.startsWith (probe.getPath ()))
{
cookies.insertElementAt (cookie, j);
break ;
}
}
}
if (isNewCookie == true )
{
cookies.addElement(cookie);
}
}
else
{ // new cookie list needed
cookies = new Vector ();
cookies.addElement (cookie);
mCookieJar.put (domain, cookies);
}
}
{
String path;
Vector cookies;
Cookie probe;
if ( null != cookie.getDomain ())
domain = cookie.getDomain ();
path = cookie.getPath ();
if ( null == mCookieJar)
mCookieJar = new Hashtable (); // turn on
cookie processing
cookies = (Vector)mCookieJar.get (domain);
if ( null != cookies)
{
boolean isNewCookie = true ;
for ( int j = 0 ; j < cookies.size (); j ++ )
{
probe = (Cookie)cookies.elementAt (j);
if (probe.getName ().equalsIgnoreCase
(cookie.getName ()))
{
if (isNewCookie == true ) isNewCookie =
false ;
// we keep paths sorted most specific to
least
if (probe.getPath ().equals (path))
{
cookies.setElementAt (cookie, j); //
replace
break ;
}
else if (path.startsWith (probe.getPath ()))
{
cookies.insertElementAt (cookie, j);
break ;
}
}
}
if (isNewCookie == true )
{
cookies.addElement(cookie);
}
}
else
{ // new cookie list needed
cookies = new Vector ();
cookies.addElement (cookie);
mCookieJar.put (domain, cookies);
}
}