UTF-8字符串-if / else if不起作用,但值等于

时间:2018-07-08 11:44:04

标签: c# visual-studio

我对C#和if / else if字符串值有疑问。

它将返回正确的值,但是会对其进行检查,并且还会抛出其他错误(如果登录成功)

代码:

public void UserLogin(string Username, string Password)
     {
         ConsoleLog Log = new ConsoleLog();
         WebClient client = new WebClient();
         ServerURL = "http://play.projectzeternity.tk/logonapi.php?login&user=" + Username + "&password=" + Password; // Logon API
         byte[] html = client.DownloadData(ServerURL);
         UTF8Encoding utf = new UTF8Encoding();
         string response = utf.GetString(html); // Here is string - API Response
         string check = Convert.ToString(response); // I tried converting to string, but it's not working.
         Log.Log("Checking response...");
         if (check == "UserNotExist") // User not exist = Successfull login
         {
             Log.Log("Response: UserNotExist");
             ShowError("User doesn't exist!");
         }
         else if (check == "BadArguments") // == Successfull login
         {
             Log.Log("Response: BadArguments");
             ShowError("Invalid arguments!");
         }
         else if (check == "PlayerIsBanned") // == Successfull login
         {
             Log.Log("Response: PlayerIsBanned");
             ShowError("This account is banned for breaking rules!");
         }
         else if (check == "WrongPassword") // = Successfull login
         {
             Log.Log("Response: WrongPassword");
             ShowError("Wrong password!");
         }
         else if (check == "UserAlreadyExist") // = Successfull login
         {
             Log.Log("Response: UserAlreadyExist");
             ShowError("User already exists!");
         }
         else // Successfull login
         {
             Log.Log("Response: " + check);
             IsLoggedIn = true;
             LoginForm.SetActive(false);
             RegisterForm.SetActive(false);
             ConnectionStatus.SetActive(true);
             ConnectionStatusText.text = "Connecting to master...";
             PhotonNetwork.ConnectUsingSettings(GameOperations.PZVersion);
             PhotonNetwork.playerName = Name;
             PhotonNetwork.JoinLobby();
         }
         Log.Log("API response: " + response);
         Log.Log("Converted response: " + check);
         Name = Username;
     }

它将加入API,API返回响应,而我将其输入if / else,并且值为null,为什么?

如果我将打印来自API(Debug.Log)的响应,它将返回正确的响应...

我在Unity Awsners上提问,但没有人回应。 :(

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用WebClient.DownloadString()直接从WebClient请求中获取字符串,而无需处理原始字节数据:

 public void UserLogin(string Username, string Password)
 {
     ConsoleLog Log = new ConsoleLog();
     WebClient client = new WebClient();
     ServerURL = "http://play.projectzeternity.tk/logonapi.php?login&user=" + Username + "&password=" + Password; // Logon API
     string check = client.DownloadString(ServerURL);

     // validate the response here...
 }