博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

不要使用 reader.Peek() 去读取每行数据

Posted on 2016-02-19 15:14  随遇  阅读(2014)  评论(0编辑  收藏  举报

1.问题描述

使用SteamRead的Peek()和ReadLine()来读取流中的数据,如果数据行数太多,会读取不完整(后面有些数据就读不出来了)。

比如:

while (srResponseReader.Peek() > 0)
{
  string line = srResponseReader.ReadLine();

  if (!String.IsNullOrEmpty(line))
  builder.Append(line);
}

最好使用:

while ((line = srResponseReader.ReadLine())!=null)
{

  if (!String.IsNullOrEmpty(line))
  builder.Append(line);
}

 public static string GetHttpWebResponse(string appUrl)
        {
            var wrWebRequest = WebRequest.Create(appUrl) as HttpWebRequest;
            if (wrWebRequest != null)
            {
                wrWebRequest.Timeout = wrWebRequest.Timeout * 10;
                return GetHttpWebResponse(wrWebRequest);
            }
            return null;
        }
public static string GetHttpWebResponse(HttpWebRequest wrWebRequest)
        {
            try
            {
                var hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

                if (hwrWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    using (var srResponseReader = new StreamReader(hwrWebResponse.GetResponseStream()))
                    {
                        var builder = new StringBuilder();
                        while (srResponseReader.Peek() > 0)
                        {
                            string line = srResponseReader.ReadLine();

                            if (!String.IsNullOrEmpty(line))
                                builder.Append(line);
                        }
                        srResponseReader.Close();
                        return builder.ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                //LOG
                return "";
            }
        }

 

最好使用下面的代码

  public static string GetHttpWebResponse(HttpWebRequest wrWebRequest)
        {
            try
            {
                var hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

                if (hwrWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    using (var srResponseReader = new StreamReader(hwrWebResponse.GetResponseStream()))
                    {
                        var builder = new StringBuilder();
                        while ((line = srResponseReader.ReadLine()) != null)
                        {
                            if (!String.IsNullOrEmpty(line))
                                builder.Append(line);
                        }
                        srResponseReader.Close();
                        return builder.ToString();

                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                //LOG
                return "";
            }
        }

 或者一次性读出流:srResponseReader.ReadToEnd()

相关链接:

http://stackoverflow.com/questions/9376887/c-using-streamreader-to-read-line-from-txt-file-but-peek-return-1-even-the