本文整理汇总了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);
}
示例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();
}