本文整理汇总了C#中System.Net.Http.Headers.HttpContentHeaders类的典型用法代码示例。如果您正苦于以下问题:C# HttpContentHeaders类的具体用法?C# HttpContentHeaders怎么用?C# HttpContentHeaders使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpContentHeaders类属于System.Net.Http.Headers命名空间,在下文中一共展示了HttpContentHeaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetDefaultContentHeaders
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue(ApplicationJsonMediaType);
this.SerializerSettings.Formatting = Formatting.None;
this.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
示例2: PopulateHeaders
public static void PopulateHeaders(this HttpPacket packet, HttpContentHeaders contentHeaders, HttpHeaders generalHeaders)
{
if (packet == null) throw new ArgumentNullException("packet");
string hdrKey;
foreach (var hdr in packet.Headers)
{
if (hdr.Key == null) continue;
hdrKey = hdr.Key.Trim().ToUpperInvariant();
if (hdrKey == "CONTENT-LENGTH") continue; //Content Length is automaitically calculated
if (Array.IndexOf<String>(contentOnlyHeaders, hdrKey) >= 0)
{
//TODO: Confirm if HttpResponseMessage/HttpRequestMessage will break headers into "," commas whereas in actuality header in Packet is an entire header
contentHeaders.Add(hdr.Key.Trim(), hdr.Value);
}
else
{
generalHeaders.Add(hdr.Key.Trim(), hdr.Value);
}
//TODO: Check if a string can be parsed properly into the typed header
//Test adding multiple headers of the same name will do. // Look up the Add overload that takes an ienumerable<string> to figure out its purpose.
}
}
示例3: GetStream
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null)
{
throw Error.ArgumentNull("parent");
}
if (headers == null)
{
throw Error.ArgumentNull("headers");
}
string localFilePath;
try
{
string filename = this.GetLocalFileName(headers);
localFilePath = Path.Combine(this._rootPath, Path.GetFileName(filename));
}
catch (Exception e)
{
throw Error.InvalidOperation(e, Resources.MultipartStreamProviderInvalidLocalFileName);
}
// Add local file name
MultipartFileData fileData = new MultipartFileData(headers, localFilePath);
this._fileData.Add(fileData);
return File.Create(localFilePath, this._bufferSize, FileOptions.Asynchronous);
}
示例4: ContentLength_AddInvalidValueUsingUnusualCasing_ParserRetrievedUsingCaseInsensitiveComparison
public void ContentLength_AddInvalidValueUsingUnusualCasing_ParserRetrievedUsingCaseInsensitiveComparison()
{
_headers = new HttpContentHeaders(new ComputeLengthHttpContent(() => 15));
// Use uppercase header name to make sure the parser gets retrieved using case-insensitive comparison.
Assert.Throws<FormatException>(() => { _headers.Add("CoNtEnT-LeNgTh", "this is invalid"); });
}
示例5: IsFileContent
public static bool IsFileContent(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null)
{
throw new ArgumentNullException("parent");
}
if (headers == null)
{
throw new ArgumentNullException("headers");
}
// For form data, Content-Disposition header is a requirement.
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition == null)
{
// If no Content-Disposition header was present.
throw new InvalidOperationException("No Content-Disposition header found");
}
// The file name's existence indicates it is a file data.
if (!string.IsNullOrEmpty(contentDisposition.FileName))
{
return true;
}
return false;
}
示例6: CopyHeaders
private static void CopyHeaders(HttpContentHeaders fromHeaders,
HttpContentHeaders toHeaders)
{
foreach (KeyValuePair<string, IEnumerable<string>> header in fromHeaders) {
toHeaders.Add(header.Key, header.Value);
}
}
示例7: OnWriteToStreamAsync
protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream,
HttpContentHeaders contentHeaders,
FormatterContext formatterContext,
TransportContext transportContext)
{
string callback;
if (IsJsonpRequest(formatterContext.Response.RequestMessage, out callback))
{
return Task.Factory.StartNew(() =>
{
var writer = new StreamWriter(stream);
writer.Write(callback + "(");
writer.Flush();
base.OnWriteToStreamAsync(type, value, stream, contentHeaders,
formatterContext, transportContext).Wait();
writer.Write(")");
writer.Flush();
});
}
else
{
return base.OnWriteToStreamAsync(type, value, stream, contentHeaders, formatterContext, transportContext);
}
}
示例8: AddRange
public void AddRange(HttpContentHeaders headers)
{
foreach (var header in headers)
{
this._headers.Add(header.Key, header.Value);
}
}
示例9: OnWriteToStreamAsync
protected override System.Threading.Tasks.Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
{
return new TaskFactory().StartNew(() =>
{
new StreamWriter(stream).Write((string)value);
});
}
示例10: OnReadFromStreamAsync
protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext)
{
return new TaskFactory<object>().StartNew(() =>
{
return new StreamReader(stream).ReadToEnd();
});
}
示例11: GetStream
/// <summary>
/// This body part stream provider examines the headers provided by the MIME multipart parser
/// and decides whether it should return a file stream or a memory stream for the body part to be
/// written to.
/// </summary>
/// <param name="parent">The parent MIME multipart HttpContent instance.</param>
/// <param name="headers">Header fields describing the body part</param>
/// <returns>The <see cref="Stream"/> instance where the message body part is written to.</returns>
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null)
{
throw Error.ArgumentNull("parent");
}
if (headers == null)
{
throw Error.ArgumentNull("headers");
}
// For form data, Content-Disposition header is a requirement
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition != null)
{
// If we have a file name then write contents out to temporary file. Otherwise just write to MemoryStream
if (!String.IsNullOrEmpty(contentDisposition.FileName))
{
// We won't post process files as form data
_isFormData.Add(false);
return base.GetStream(parent, headers);
}
// We will post process this as form data
_isFormData.Add(true);
// If no filename parameter was found in the Content-Disposition header then return a memory stream.
return new MemoryStream();
}
// If no Content-Disposition header was present.
throw Error.InvalidOperation(Properties.Resources.MultipartFormDataStreamProviderNoContentDisposition, "Content-Disposition");
}
示例12: SetContentType
public virtual void SetContentType(Type type, HttpContentHeaders headers, string mediaType)
{
if (this.SupportedMediaTypes == null) {
throw new InvalidOperationException(string.Format("{0} does not set support media types", base.GetType()));
}
headers.ContentType = this.SupportedMediaTypes.Contains(mediaType) ? mediaType : this.SupportedMediaTypes.First();
}
示例13: ContentLength_UseAddMethod_AddedValueCanBeRetrievedUsingProperty
public void ContentLength_UseAddMethod_AddedValueCanBeRetrievedUsingProperty()
{
_headers = new HttpContentHeaders(() => { Assert.True(false, "Delegate called."); return 0; });
_headers.TryAddWithoutValidation(HttpKnownHeaderNames.ContentLength, " 68 \r\n ");
Assert.Equal(68, _headers.ContentLength);
}
示例14: GetStream
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
Stream stream = null;
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition != null)
{
if (!String.IsNullOrWhiteSpace(contentDisposition.FileName))
{
string connectionString = ConfigurationManager.AppSettings["azureConnectionString"];
var containerName = ConfigurationManager.AppSettings["container"];
if (contentDisposition.Name.Contains("avatar"))
containerName = "avatar";
if (contentDisposition.Name.Contains("headerImage"))
containerName = "header";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(Guid.NewGuid().ToString() + ".jpg");
blob.Metadata.Add("source", ConfigurationManager.AppSettings["source"]);
if (_breweryDto != null) blob.Metadata.Add("breweryId", _breweryDto.Id.ToString());
if(_userDto != null) blob.Metadata.Add("username", _userDto.Username);
stream = blob.OpenWrite();
headers.ContentDisposition.FileName = blob.Name;
}
}
return stream;
}
示例15: GetStream
/// <summary>
/// <para>This method assumes that all form segments come before the actual file data.</para>
/// <para>Otherwise, GetLocalFileName will fail.</para>
/// </summary>
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (headers.ContentDisposition.FileName == null)
{
return base.GetStream(parent, headers);
}
string flowFileName = null;
try
{
flowFileName = GetLocalFileName(headers);
}
catch (Exception ex)
{
throw new Exception("Flow chunk information was not properly transmitted before the chunk payload.", ex);
}
FileStream flowFileStream;
var path = Path.Combine(RootPath, flowFileName);
flowFileStream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
flowFileStream.SetLength(MetaData.FlowTotalSize);
flowFileStream.Seek(MetaData.FileOffset, 0);
return flowFileStream;
}