错误地检查响应c# [英] incorrectly checks the response c#

查看:186
本文介绍了错误地检查响应c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code that gets a response from the POST request:

//Запрос авторизации бухаря
        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

My code called just 2 functions, i.e. show 2 MessageBox

如何解决?

更新

推荐答案

因为,如果 responseFromServer 变量等于Foo那么它适合两种情况,因为它不等于Bad Login并且不等于旧版本

Because if, say, responseFromServer variable equals to "Foo" then it fits both conditions, because it's not equal to " Bad Login" and not equal to " Old version"

你可能想要的是否则如果

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

或许你想要 == 而不是!=

if (responseFromServer == " Bad Login")

因此,现在就找出你真正想要实现的目标。

So it's your job now to find out what you really want to achieve.

PS:两个字符串的开头都是空格?

PS: are spaces in the beginning of both strings intended?

这篇关于错误地检查响应c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆