c#忽略else if语句

时间:2017-11-14 12:42:23

标签: c# winforms

我正在尝试创建一个winforms应用程序,以便在点击某些内容时检查网页是否有响应。

我已经测试了网页以查看它是否是PHP错误,但从那一方面它可以正常工作。

它完全忽略else if语句并跳过它下面的else语句,即使响应是"未分配"。

这是我的代码:

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);
            }
        }
    }

2 个答案:

答案 0 :(得分:4)

不,它没有被忽视。您正在通过调用if来阅读第一个reader.ReadToEnd()块中的所有数据。这样,您的else if声明中没有可供阅读的更多数据;它返回空字符串。因此条件不匹配,最终else块被执行。

修改如下代码。请注意以下代码中的临时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
}

答案 1 :(得分:2)

您的一般逻辑中存在错误。当您输入第一个if语句时,您将使用代码reader.ReadToEnd()读取到流的末尾。在下一个语句(else)中,您正在再次读取流,但它已被读取,因此它将返回一个空字符串,因此最后一个else语句将被有效地命中。

您还可以在MSDN上阅读此内容:StreamReader.ReadToEnd() Method

返回值的定义:

  

流的其余部分作为字符串,从当前位置到结束。如果当前位置位于流的末尾,则返回空字符串(“”)。

您的代码应如下所示:

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);
}