User talk:Gracenotes/Java code
Appearance
Problems
I've tried this bot on Wikibooks, and I've got two problems:
- Impossible to login, whereas it works with the same user and password in my Python scripts.
- When I comment the two return; supposed to block the editions when not logged, I've got this:
C:\Program Files (x86)\Java\bot>java WikiEditTest
Not logged in
Not logged in.
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)
at WikiEdit.editPage(WikiEdit.java:99)
at WikiEditTest.main(WikiEditTest.java:17)
JackPotte (talk) 21:44, 12 February 2012 (UTC)
- The following code seems to contains the bug:
while ((headerName = connection.getHeaderFieldKey(++i)) != null)
{
headerName = connection.getHeaderFieldKey(i);
if (headerName != null && headerName.equalsIgnoreCase("Set-Cookie"))
{
receivedCookie.append("; " + connection.getHeaderField(i).split(";")[0]);
}
}
receivedCookie.delete(0, 2);
- The getHeaderFieldKey is called twice and first called with i == 1 (because ++ before).
- Also the last line delete the first two characters even if the string is empty (-> exception will be thrown).
- Try this instead:
while ((headerName = connection.getHeaderFieldKey(i)) != null)
{
if (headerName != null && headerName.equalsIgnoreCase("Set-Cookie"))
{
if (receivedCookie.length()>0) receivedCookie.append("; ");
receivedCookie.append(connection.getHeaderField(i).split(";")[0]);
}
i++;
}
// receivedCookie.delete method call removed
- -- ◄ David L • discuter ► 09:15, 13 February 2012 (UTC)