简体   繁体   English

C#忽略其他if语句

[英]c# ignoring else if statement

I am trying to make a winforms application so when something is clicked it checks a webpage for it's response. 我正在尝试制作winforms应用程序,因此当单击某些内容时,它将检查网页的响应。

I have tested the web page to see if it is a PHP error but it works fine from that side. 我已经测试过该网页,看是否是PHP错误,但从那方面来看效果很好。

It is completely ignoring the else if statement and skips to the else statement below it even though the response is "Unassigned". 即使响应是“未分配的”,它也完全忽略了else if语句,并跳到它下面的else语句。

Here is my code: 这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://fms.psrpc.co.uk/apiconfirmD.php?" + ApiKey);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (response)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            if (reader.ReadToEnd() == "Changed")
            {
                label2.Visible = false;
                button1.Enabled = false;
                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button8.Enabled = true;
                timer1.Enabled = true;
            }
            else if (reader.ReadToEnd() == "Unassigned")
            {
                string message = "Error Code: B66794O37945O46791K@@Error booking on.@Please make sure you have been assigned to a vehicle.@@If this error persists please contact K.McCrudden.";
                message = message.Replace("@", "" + System.Environment.NewLine);
                string title = "Error!";

                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
            }
            else
            {
                string message = "Error Code: B002875O46883O84655K@@Error booking on.@Please make sure you have booked a shift and have been assigned.@@If this error persists please contact K.McCrudden.";
                message = message.Replace("@", "" + System.Environment.NewLine);
                string title = "Error!";

                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
            }
        }
    }

No, it is not being ignored. 不,它没有被忽略。 You are reading all the data in your first if block by calling reader.ReadToEnd() . 您正在通过调用reader.ReadToEnd()来读取第一个if块中的所有数据。 This way, there is no further data available to read in your else if statement; 这样,您的else if语句中就没有其他数据可读取; it returns empty string. 它返回空字符串。 Thus condition does not match and final else block gets executed. 因此条件不匹配,并且最终的else块被执行。

Modify the code something like below. 修改代码,如下所示。 Notice the temporary data variable in below code. 请注意以下代码中的临时data变量。

StreamReader reader = new StreamReader(response.GetResponseStream());
string data = reader.ReadToEnd();//Read the data in temp variable.
//Use this variable to check the conditions further.
if (data == "Changed")
{
    //Your code here
}
else if (data == "Unassigned")
{
    //Your code here
}
else
{
    //Your code here
}

You have an error in your general logic. 您的一般逻辑有误。 When you enter the first if statement, you're reading to the end of the stream with the code reader.ReadToEnd() . 输入第一个if语句时,您将使用代码reader.ReadToEnd()读取流的末尾。 In the next statement (the else ), you're reading the stream again, but it has already been read, so it will return an empty string, thus the last else statement will effectively be hit. 在下一个语句( else )中,您将再次读取流,但是它已经被读取,因此它将返回一个空字符串,因此最后一个else语句将被有效地命中。

You can also read about this on MSDN: StreamReader.ReadToEnd() Method . 您也可以在MSDN上阅读有关此内容: StreamReader.ReadToEnd()方法

Definition of the return value: 返回值的定义:

The rest of the stream as a string, from the current position to the end. 流的其余部分作为字符串,从当前位置到末尾。 If the current position is at the end of the stream, returns an empty string (""). 如果当前位置在流的末尾,则返回一个空字符串(“”)。

Your code should look like this: 您的代码应如下所示:

StreamReader reader = new StreamReader(response.GetResponseStream());
var result = reader.ReadToEnd();

if(result == "Changed")
{
    label2.Visible = false;
    button1.Enabled = false;
    button2.Enabled = true;
    button3.Enabled = true;
    button4.Enabled = true;
    button5.Enabled = true;
    button6.Enabled = true;
    button7.Enabled = true;
    button8.Enabled = true;
    timer1.Enabled = true;
}
else if(result == "Unassigned")
{
    string message = "Error Code: B66794O37945O46791K@@Error booking on.@Please make sure you have been assigned to a vehicle.@@If this error persists please contact K.McCrudden.";
    message = message.Replace("@", "" + System.Environment.NewLine);
    string title = "Error!";

    MessageBoxButtons buttons = MessageBoxButtons.OK;
    DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
}
else
{
    string message = "Error Code: B002875O46883O84655K@@Error booking on.@Please make sure you have booked a shift and have been assigned.@@If this error persists please contact K.McCrudden.";
    message = message.Replace("@", "" + System.Environment.NewLine);
    string title = "Error!";

    MessageBoxButtons buttons = MessageBoxButtons.OK;
    DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM