C#Stream不断跳过第一行

时间:2017-01-16 05:22:23

标签: c# streamreader webresponse

好吧,我正在做一些应该相当简单的事情,我相信我在这里忽略了一些东西。

好吧,我使用HttpWebRequest和WebResponse来检测服务器上是否存在Robots.txt(并且完全正常)。但是,我试图添加到myList.Add(reader.ReadLine());哪(工作)。但问题是,它一直在跳过第一行。

https://www.assetstore.unity3d.com/robots.txt<那是我开始注意到的问题(只是让你知道我在说什么)。它仅用于测试目的。 (看看那个链接,这样你就可以了解我在说什么)。

Anywho,它也没有将reader.ReadLine添加到我的列表中(仅限第一行)。所以我并不完全理解发生了什么,我已经尝试过这样做,我发现的唯一的事情是故意想跳过一条线,我不想这样做。

我的代码如下。

Console.WriteLine("Robots.txt Found: Presenting Rules in (Robot Rules).");
                HttpWebRequest getResults = (HttpWebRequest)WebRequest.Create(ur + "robots.txt");
                WebResponse getResponse = getResults.GetResponse();
                using (StreamReader reader = new StreamReader(getResponse.GetResponseStream())) {
                    string line = reader.ReadLine();
                    while(line != null && line != "#") {
                        line = reader.ReadLine();
                        rslList.Add(line);
                        results.Text = results.Text + line + Environment.NewLine; // At first I thought it might have been this (nope).
                    }


                    // This didn't work either (figured perhaps maybe it was skipping because I had to many things.
                    // So I just put into a for loop, - nope still skips first line.
                    // for(int i = 0; i < rslList.Count; i++) {
                    //     results.Text = results.Text + rslList[i] + Environment.NewLine;
                    // }
                }
                // Close the connection sense it is no longer needed.
                getResponse.Close();
                // Now check for user-rights.
                CheckUserRights();

结果图。 enter image description here

1 个答案:

答案 0 :(得分:1)

下次调用读取行时更改

var line = reader.ReadLine(); //Read first line
while(line != null && line != "#") { //while line condition satisfied
    //perform your desired actions
    rslList.Add(line);
    results.Text = results.Text + line + Environment.NewLine; 
    line = reader.ReadLine(); //read the next line
}