在我的 midlet 中验证用户名和密码时出现问题
我已经修改了登录并实现了这一点。
public void login() {
HttpConnection c = null;
InputStream is = null;
StringBuffer str = new StringBuffer();
String url = "http://101.00.00.000/...../"
+ "?user=" + getUsername().getString().trim()
+ "&password=" + getPass().getString().trim();
try {
c = (HttpConnection) Connector.open(url);
is = c.openInputStream();
int len = (int)c.getLength();
int ch;
String getS = "";
while((ch = is.read()) != -1){
str.append((char)ch);
getS = str.toString();
String msg = "Login successful";
System.out.println(getS.substring(0, getS.length()));
if (getS.substring(0, 16).equals(msg)){
System.out.println(getS);
switchDisplayable(null, svgMenu.getSvgCanvas());
}else{
switchDisplayable(null, getAlert2());
// }
}
System.out.println(getS);
}
catch (IOException exception) {
try {
if (is != null)
is.close();
if (c != null)
c.close();
exception.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
调试时 url 的响应给出了登录成功(即在命令行中)的正确答案。但我注意到字符串比较没有返回正确的逻辑 即
if (getS.substring(0, 16).equals(msg)){
System.out.println(getS);
switchDisplayable(null, svgMenu.getSvgCanvas());
}
if 语句永远不会被执行。我该怎么办。
I have modified the login in and implemented this instead.
public void login() {
HttpConnection c = null;
InputStream is = null;
StringBuffer str = new StringBuffer();
String url = "http://101.00.00.000/...../"
+ "?user=" + getUsername().getString().trim()
+ "&password=" + getPass().getString().trim();
try {
c = (HttpConnection) Connector.open(url);
is = c.openInputStream();
int len = (int)c.getLength();
int ch;
String getS = "";
while((ch = is.read()) != -1){
str.append((char)ch);
getS = str.toString();
String msg = "Login successful";
System.out.println(getS.substring(0, getS.length()));
if (getS.substring(0, 16).equals(msg)){
System.out.println(getS);
switchDisplayable(null, svgMenu.getSvgCanvas());
}else{
switchDisplayable(null, getAlert2());
// }
}
System.out.println(getS);
}
catch (IOException exception) {
try {
if (is != null)
is.close();
if (c != null)
c.close();
exception.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
The response from the url on debugging gave a right answer that the login was successfull i.e in the commandline. But I noticed that the string comparison doesn't return a correct logic
i.e
if (getS.substring(0, 16).equals(msg)){
System.out.println(getS);
switchDisplayable(null, svgMenu.getSvgCanvas());
}
The if statement never gets to execute. What do I do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在服务器端犯了一个错误。因为您从客户端(移动设备)发送用户名和密码的字符串值。您需要在服务器端进行验证。在此之前,您需要检查客户端(移动设备)的用户名和密码是否有空格。使用 getUsername().getString().trim() 和 getPass().getString().trim() ,如果需要检查区分大小写。
You made a mistake on server side. Because you sending the string value of username and password from client side(mobile). you need to validate on server side. Before that you need to check if any white space on username and password on client side(mobile). use
getUsername().getString().trim()
andgetPass().getString().trim()
and also If need check the case sensitive.