0

我有一个从 POST 请求中获得响应的代码:

//Запрос авторизации бухаря
        WebRequest request = WebRequest.Create("http://clannr.org/scripts/login/auth.php");
        request.Method = "POST";
        string postData = "user=" + Login.Text + "&password=" + Password.Text + "&version=13";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();

但是在检查响应时:

        Match match = regex.Match(responseFromServer);

if (match.Success)
        {
            var input = responseFromServer;
            var split = input.Split(':');
            var final = split[3];
            ProcessStartInfo mcStartInfo = new ProcessStartInfo("javaw", "-Xms1024m -Xmx1024m -cp \"" + appData + "\\.ClanNR\\bin\\minecraft1.jar;" + appData + "\\.ClanNR\\bin\\jinput.jar;" + appData + "\\.ClanNR\\bin\\lwjgl.jar;" + appData + "\\.ClanNR\\bin\\lwjgl_util.jar \" -Djava.library.path=\"" + appData + "\\.ClanNR\\bin\\natives\" net.minecraft.client.Minecraft" + " " + username + " " + final + " " + server1);
            Process.Start(mcStartInfo);
            this.Close();



        }

        else if (responseFromServer == " Bad Login")
        {
            MessageBox.Show("Uncorrect login/password!");
        }
        else if (responseFromServer == " Old version")
        {
            MessageBox.Show("Launcher is old!");
        }

我的代码只调用了 2 个函数,即 show 2 MessageBox

如何解决?

更新

4

2 回答 2

2

因为如果,比如说,responseFromServer变量等于,"Foo"那么它同时满足这两个条件,因为它不等于" Bad Login"也不等于" Old version"

你可能想要的是else if

    if (responseFromServer != " Bad Login")
    {
        MessageBox.Show("Uncorrect login/password!");
    }
    else if (responseFromServer != " Old version")
    {
        MessageBox.Show("Launcher is old!");
    }

或者你可能想要==而不是!=

if (responseFromServer == " Bad Login")

所以现在你的工作就是找出你真正想要实现的目标。

PS:两个字符串开头的空格是有意的吗?

于 2012-11-04T22:33:31.577 回答
1

首先,!=运算符表示不相等。
如果我错了,请纠正我,但我认为你想使用==.

if (responseFromServer == " Bad Login")
{
    MessageBox.Show("Uncorrect login/password!");
}

else if (responseFromServer == " Old version")
{
    MessageBox.Show("Launcher is old!");
}

其次,你应该使用 else if。如果第一个条件为真,那么它不会检查另一个。

于 2012-11-04T22:35:43.097 回答