zoukankan      html  css  js  c++  java
  • 不要使用 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

  • 相关阅读:
    APP测试中 iOS 和 Android有哪些区别呢
    软件测试的标准工作流程
    selenium在元素定位的时候,明明定位到元素却始终不可见
    接口测试要点
    测试用例的组成部分
    APP测试的全面性
    安卓出现ARN的原因
    测试的多个方面
    α测试和β测试
    接口自动化测试用例设计方法
  • 原文地址:https://www.cnblogs.com/weibozeng/p/5201107.html
Copyright © 2011-2022 走看看