i want to create license system in java.
I created function to check if license is true.
My code:
private static boolean isPurchased(String license)
{
try
{
URL url = new URL("http://mineverse.pl/haslicense.php?license=" + license);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
in.close();
return Boolean.valueOf(str);
} catch (Exception e)
{
e.printStackTrace();
}
return false;
}
and chceck function onenable
if(this.isPurchased(license)){
String license = cfg.getString("Licensing_System.License");
System.out.println("Licencja" + license + " kupiona! Dziekujemy!");
System.out.println(this.isPurchased(license));
}else {
System.out.println("Licencja zostala sfalszowana! Zglaszam to do serwera autoryzacji!");
}
And my link:
http://mineverse.pl/haslicense.php?license=diverse12345
as you can see this link return true, (i did echo 'true';) but java console always return false (i want true because website have true at this link) and it logs:
Licencja zostala sfalszowana! Zglaszam to do serwera autoryzacji!
Whats wrong? How can i return true on my website to allow java learn this boolean?>
That's because your server is not just returning True or False. It is returning this instead:
<html>
</html>
True
and your code is only reading the first line <html>
and parsing it as a boolean, which results in false.
To fix it, either read the whole body looking for True/False or return just True/False on your body.
The following code should work with the current html even if it contains the <html>
tags:
URL url = new URL("http://mineverse.pl/haslicense.php?license=" + license);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = null;
boolean ret = false;
while ((str = in.readLine()) != null) {
str = str.toLowerCase();
if (str.contains("true")) {
ret = true;
break;
}
}
in.close();
return ret;
I tried your link http://mineverse.pl/haslicense.php?license=diverse12345 and it returns this:
<html>
</html>
True
When you pass that to Boolean.valueOf(...)
, the result will be false
. The method Boolean.valueOf(...)
will only return true
if the string you pass it consists of exactly four characters: true
.
You need to get rid of the HTML tags, the spaces and newlines, and case is also important; True
will not work, it must be true
.