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


    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

  • 相关阅读:
    NOIP 2016 回文日期
    USACO Ski Course Design
    USACO Combination Lock
    USACO 利润Profits
    POJ 3368 Frequent values
    USACO Balanced Lineup
    JDOJ 1065 打倒苏联修正主义
    JDOJ 2174 忠诚
    VIJOS-P1514 天才的记忆
    VIJOS-P1423 最佳路线
  • 原文地址:https://www.cnblogs.com/weibozeng/p/5201107.html
Copyright © 2020-2023  润新知