当前位置: 首页>>代码示例>>C#>>正文


C# DataReader.ReadLine方法代码示例

本文整理汇总了C#中DataReader.ReadLine方法的典型用法代码示例。如果您正苦于以下问题:C# DataReader.ReadLine方法的具体用法?C# DataReader.ReadLine怎么用?C# DataReader.ReadLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataReader的用法示例。


在下文中一共展示了DataReader.ReadLine方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadResponseHeader

        /// <summary>
        /// Read the response header
        /// </summary>
        /// <param name="reader">The data reader to read from</param>        
        /// <param name="strictParsing">Use strict parsing rules</param>
        /// <param name="logger">The logger to write errors to</param>
        /// <exception cref="EndOfStreamException">Thrown when stream ends</exception>
        /// <returns>The http response header</returns>
        public static HttpResponseHeader ReadResponseHeader(DataReader reader, bool strictParsing, Logger logger)
        {
            // Read just first 4 chars
            string header = reader.ReadLine(BinaryEncoding.Instance, TextLineEnding.LineFeed, 4);

            // If no more data left then we end here
            if (header.Length == 0)
            {
                throw new EndOfStreamException();
            }

            if (strictParsing)
            {
                CheckLineEnding(header);
            }

            if (header.Equals("http", StringComparison.OrdinalIgnoreCase))
            {
                // Read to end of line
                header = header + reader.ReadLine();

                HttpVersion ver = new HttpVersion(1, 0);
                string[] values = header.Trim().Split(new char[] { ' ' }, 3);

                if (values.Length < 2)
                {
                    throw new HttpStreamParserException(String.Format(Properties.Resources.HttpParser_ResponseHeaderInvalid, header));
                }

                if (!HttpVersion.TryParse(values[0], out ver))
                {
                    throw new HttpStreamParserException(Properties.Resources.HttpParser_InvalidHttpVersionString);
                }

                int responseCode;

                if (!int.TryParse(values[1], out responseCode))
                {
                    throw new HttpStreamParserException(Properties.Resources.HttpParser_InvalidHttpResponseCode);
                }

                return new HttpResponseHeader(reader, ReadHeaders(reader, strictParsing, logger),
                    responseCode, values.Length > 2 ? values[2] : String.Empty, ver);
            }
            else
            {
                // Case where server probably responded with simple response even when we sent a full one
                return new HttpResponseHeader(reader, header);
            }
        }
开发者ID:michyer,项目名称:canape,代码行数:58,代码来源:HttpParser.cs

示例2: ReadHeaders

        private static IEnumerable<KeyDataPair<string>> ReadHeaders(DataReader reader, bool strictParsing, Logger logger)
        {
            while (true)
            {
                string line = reader.ReadLine();

                if (line.Length == 0)
                {
                    throw new EndOfStreamException();
                }

                if (strictParsing)
                {
                    CheckLineEnding(line);
                }

                // Technically this probably should be TrimEnd but for reasons of compatibilty probably best left
                line = line.Trim();

                if (line.Length == 0)
                {
                    break;
                }

                string[] values = line.Split(new char[] { ':' }, 2);
                string name = String.Empty;
                string value = String.Empty;

                if (strictParsing && values.Length != 2)
                {
                    throw new HttpStreamParserException(String.Format(Properties.Resources.HttpParser_InvalidHeaderException, line));
                }
                else
                {
                    if(values.Length > 0)
                    {
                        name = values[0];
                        if(values.Length > 1)
                        {
                            value = values[1];
                        }
                    }
                }

                yield return new KeyDataPair<string>(name.Trim(), value.Trim());
            }
        }
开发者ID:michyer,项目名称:canape,代码行数:47,代码来源:HttpParser.cs

示例3: ReadRequestHeader

        /// <summary>
        /// Read a request header
        /// </summary>
        /// <param name="reader">The reader to use</param>
        /// <param name="strictParsing">Use strict parsing rules</param>
        /// <param name="logger">The logger to write errors to</param>    
        /// <param name="prefix">Some prefix characters, if null then just reads whole line</param>
        /// <exception cref="EndOfStreamException">Thrown when stream ends</exception>
        /// <returns>The request header object</returns>
        public static HttpRequestHeader ReadRequestHeader(DataReader reader, bool strictParsing, Logger logger, char[] prefix)
        {
            string header;

            if(prefix != null)
            {
                header = new string(prefix) + reader.ReadLine();
            }
            else
            {
                header = reader.ReadLine();
            }

            if (strictParsing)
            {
                CheckLineEnding(header);
            }

            // Let us default to version unknown
            HttpVersion ver = HttpVersion.VersionUnknown;
            List<KeyDataPair<string>> headers = new List<KeyDataPair<string>>();

            string[] values = header.Trim().Split(new char[] { ' ' }, 3);
            if (values.Length < 2)
            {
                throw new HttpStreamParserException(String.Format(Properties.Resources.HttpParser_RequestHeaderInvalid, header));
            }

            if (values.Length > 2)
            {
                if (!HttpVersion.TryParse(values[2], out ver))
                {
                    throw new HttpStreamParserException(Properties.Resources.HttpParser_InvalidHttpVersionString);
                }

                headers.AddRange(ReadHeaders(reader, strictParsing, logger));
            }

            return new HttpRequestHeader(reader, headers, values[0], values[1], ver);
        }
开发者ID:michyer,项目名称:canape,代码行数:49,代码来源:HttpParser.cs

示例4: HTTPData

        public HTTPData(DataReader stm, Logger logger)
        {
            string header = stm.ReadLine();
            int contentLength = -1;
            bool chunked = false;
            byte[] body = null;
            bool connClose = false;

            if (header.Length == 0)
            {
                throw new EndOfStreamException();
            }

            HttpVersion ver = OnReadHeader(header.Trim(), logger);

            ReadHeaders(stm, logger);

            foreach (KeyDataPair<string> value in Headers)
            {
                if (value.Name.Equals("content-length", StringComparison.OrdinalIgnoreCase))
                {
                    contentLength = int.Parse(value.Value.ToString());
                    break;
                }
                else if (value.Name.Equals("transfer-encoding", StringComparison.OrdinalIgnoreCase))
                {
                    if (value.Value.ToString().Equals("chunked", StringComparison.OrdinalIgnoreCase))
                    {
                        chunked = true;
                        break;
                    }
                }
                else if (value.Name.Equals("connection", StringComparison.OrdinalIgnoreCase))
                {
                    // If the server indicates it will close the connection afterwards
                    if (value.Value.ToString().Equals("close", StringComparison.OrdinalIgnoreCase))
                    {
                        connClose = true;
                    }
                }
            }

            body = new byte[0];

            if (CanHaveBody())
            {
                if (chunked)
                {
                    List<byte> bodyData = new List<byte>();
                    byte[] data = ReadChunk(stm, logger);

                    while (data.Length > 0)
                    {
                        bodyData.AddRange(data);

                        data = ReadChunk(stm, logger);
                    }

                    body = bodyData.ToArray();
                }
                else if (contentLength > 0)
                {
                    body = stm.ReadBytes(contentLength);
                }
                else if ((contentLength < 0) && ((ver == HttpVersion.Http1_0) || connClose) && !IsRequest)
                {
                    // HTTP1.0 waits till end of stream (need to only do this on response though)
                    // Also sometimes happen on v1.1 if the client indicates that it is closing the connection
                    body = stm.ReadToEnd();
                }
            }

            Body = body;
        }
开发者ID:michyer,项目名称:canape,代码行数:74,代码来源:HTTPData.cs

示例5: ReadChunk

        private byte[] ReadChunk(DataReader stm, Logger logger)
        {
            string s = stm.ReadLine();
            if (s.Length == 0)
            {
                return new byte[0];
            }

            string lenstr = s.Trim();
            if (lenstr.Length == 0)
            {
                return new byte[0];
            }

            lenstr = lenstr.Split(new char[] { ';' }, 1)[0];
            int lenval = int.Parse(lenstr, NumberStyles.HexNumber);

            if (lenval > 0)
            {
                byte[] data = stm.ReadBytes(lenval);
                stm.ReadLine();

                return data;
            }
            else
            {
                // Read out final trailing data
                string line = stm.ReadLine();
                while ((line.Length > 0) && (line.Trim().Length > 0))
                {
                    line = stm.ReadLine();
                }
            }

            return new byte[0];
        }
开发者ID:michyer,项目名称:canape,代码行数:36,代码来源:HTTPData.cs

示例6: ReadHeaders

        public void ReadHeaders(DataReader stm, Logger logger)
        {
            List<KeyDataPair<string>> headers = new List<KeyDataPair<string>>();

            while (true)
            {
                string line = stm.ReadLine();

                if (line.Length == 0)
                {
                    throw new EndOfStreamException();
                }

                line = line.Trim();

                if (line.Length == 0)
                {
                    break;
                }

                string[] values = line.Split(new char[] { ':' }, 2);

                if (values.Length != 2)
                {
                    throw new InvalidDataException(String.Format("Header '{0}' is invalid", line));
                }

                headers.Add(new KeyDataPair<string>(values[0].Trim(), values[1].Trim()));
            }

            Headers = headers.ToArray();
        }
开发者ID:michyer,项目名称:canape,代码行数:32,代码来源:HTTPData.cs

示例7: ReadChunkedEncoding

        private static byte[] ReadChunkedEncoding(DataReader stm, out string extension)
        {
            extension = null;

            string s = stm.ReadLine();
            if (s.Length == 0)
            {
                return new byte[0];
            }

            string lenstr = s.Trim();
            if (lenstr.Length == 0)
            {
                return new byte[0];
            }

            string[] values = lenstr.Split(new char[] { ';' }, 2);

            lenstr = values[0];

            if (values.Length > 1)
            {
                extension = values[1];
            }

            int lenval = int.Parse(lenstr, NumberStyles.HexNumber);

            if (lenval > 0)
            {
                byte[] data = stm.ReadBytes(lenval);
                stm.ReadLine();

                return data;
            }
            else
            {
                // Read out final trailing data
                string line = stm.ReadLine();
                while ((line.Length > 0) && (line.Trim().Length > 0))
                {
                    line = stm.ReadLine();
                }
            }

            return new byte[0];
        }
开发者ID:michyer,项目名称:canape,代码行数:46,代码来源:HttpResponseHeader.cs


注:本文中的DataReader.ReadLine方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。