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


C# HttpRequest.AddHeader方法代码示例

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


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

示例1: request_with_body

        public void request_with_body()
        {
            var frame = new HttpRequest("POST", "/?abc", "HTTP/1.1");
            frame.AddHeader("server", "Griffin.Networking");
            frame.AddHeader("X-Requested-With", "XHttpRequest");
            frame.ContentType = "text/plain";
            frame.Body = new MemoryStream(Encoding.ASCII.GetBytes("hello queue a"));
            var expected = "POST /?abc HTTP/1.1\r\nserver: Griffin.Networking\r\nX-Requested-With: XHttpRequest\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nhello queue a";
            var buffer = new SocketBufferFake();

            var encoder = new HttpMessageEncoder();
            encoder.Prepare(frame);
            encoder.Send(buffer);
            var actual = Encoding.ASCII.GetString(buffer.Buffer, 0, buffer.Count);

            actual.Should().Be(expected);
        }
开发者ID:ricklxm,项目名称:Griffin.Framework,代码行数:17,代码来源:HttpEncoderTests.cs

示例2: HandleRequestPart

        /// <summary>
        /// A regex would be nice :)
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="resultThisFar"></param>
        public override void HandleRequestPart(byte[] stream, HttpRequest resultThisFar)
        {
            UnparsedData = stream;

            string headerName = null;
            var bytesLeft = stream.Length;
            int headerValueStartIndex = 0;
            int headerValueEndIndex = 0;
            for (var i = 0; i < stream.Length; i++, bytesLeft--)
            {
                byte currentByte = stream[i];
                if (headerName == null && currentByte == Constants.ColonByte)
                {
                    headerName = GetHeaderString(stream, headerValueEndIndex, i);
                    headerValueStartIndex = i + 1;
                }
                else if (stream[i] == Constants.CRByte)
                {
                    // Special case when /r/n is received or /r/ncontentdata
                    if (i == 0 && bytesLeft >= 2 && stream[1] == Constants.LFByte)
                    {
                        IsFinished = true;
                        IsSucceeded = true;
                        headerValueEndIndex = 2;
                        break;
                    }

                    if (headerName != null &&
                        bytesLeft >= 2 &&
                        stream[i + 1] == Constants.LFByte)
                    {
                        // Handle end of one header scenario
                        var headerValue = GetHeaderString(stream, headerValueStartIndex, i);
                        headerValue = headerValue.TrimWhitespaces();
                        try
                        {
                            resultThisFar.AddHeader(_headerFactory.Create(headerName, headerValue));
                        }
                        catch (Exception)
                        {
                            IsFinished = true;
                            IsSucceeded = false;
                            break;
                        }
                        finally
                        {
                            headerName = null;
                            headerValueStartIndex = 0;
                            headerValueEndIndex = i + 2;
                        }
                    }

                    if (bytesLeft >= 4 &&
                        stream[i + 1] == Constants.LFByte &&
                        stream[i + 2] == Constants.CRByte &&
                        stream[i + 3] == Constants.LFByte)
                    {
                        // Handle end of headers scenario
                        headerValueEndIndex = i + 4;
                        IsFinished = true;
                        IsSucceeded = true;
                        break;
                    }
                }
            }

            UnparsedData = stream.Skip(headerValueEndIndex).ToArray();
        }
开发者ID:Ferho,项目名称:restup,代码行数:73,代码来源:HeaderParser.cs


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