[HtmlParser]bug提交(含解决方案)--A bug when set cookies

本文描述了HTML解析器中关于同一域名下无法设置多个Cookies的问题,并提供了一种修复方法。通过改进ConnectionManager.setCookie方法,实现了对同一域名下多个Cookies的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

htmlParser在设置cookies时存在bug,对于相同的domain,不能设置多个cookies.下面时偶提交的bug内容及解决办法.偶英语很烂--大家别笑话.:P

There can't set many cookies to a domain,for example:

public void testSetCookies() throws Exception
{
StringurlString
= " http://sourceforge.net/projects/htmlparser " ;
Parserparser
= this .buildParser(urlString);

}

private ParserbuildParser(StringurlString) throws
Exception
{
ConnectionManagermanager
=
Parser.getConnectionManager();
Cookiecookie
= 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(Cookiecookie,Stringdomain)
{
Stringpath;
Vectorcookies;
Cookieprobe;

if ( null != cookie.getDomain())
domain
= cookie.getDomain();
path
= cookie.getPath();
if ( null == mCookieJar)
mCookieJar
= new Hashtable(); // turnon
cookieprocessing
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 ;
// wekeeppathssortedmostspecificto
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
{
// newcookielistneeded
cookies = new Vector();
cookies.addElement(cookie);
mCookieJar.put(domain,cookies);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值