我想用java创建许可证系统。
我创建了函数来检查许可是否为真。
我的代码:
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;
}和chceck函数
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!");
}我的链接是:
http://mineverse.pl/haslicense.php?license=diverse12345
正如您所看到的,这个链接返回true,(我确实回显了' true ';)但是java控制台总是返回false (我想要true,因为网站在这个链接上有真),并且它记录:
利昌嘉·索斯塔拉斯法斯佐瓦那!Zglaszam要去做serwera autoryzacji!
出什么事了?如何在我的网站上返回true让java学习这个boolean?>
发布于 2014-06-06 20:04:08
这是因为您的服务器不只是返回True或False。相反,它将返回以下内容:
<html>
</html>
True您的代码只读取第一行<html>并将其解析为布尔值,这将导致错误。
要修复它,要么阅读整个身体寻找真假,要么在你的身体上只返回真假。
即使当前html包含<html>标记,以下代码也应该可以使用它:
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;发布于 2014-06-06 20:03:07
我试过您的链接http://mineverse.pl/haslicense.php?license=diverse12345,它返回以下内容:
当您将其传递给Boolean.valueOf(...)时,结果将是false。方法Boolean.valueOf(...)只有在传递的字符串完全由四个字符组成时才会返回true:true。
您需要去掉HTML标记、空格和换行符,大小写也很重要;True不能工作,它必须是true。
https://stackoverflow.com/questions/24089704
复制相似问题