本文整理汇总了C#中System.Net.Response.SetOptions方法的典型用法代码示例。如果您正苦于以下问题:C# Response.SetOptions方法的具体用法?C# Response.SetOptions怎么用?C# Response.SetOptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Response
的用法示例。
在下文中一共展示了Response.SetOptions方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCoapResponse
/// <summary>
/// Gets the CoAP response from an incoming HTTP response. No null value is
/// returned. The response is created from a predefined mapping of the HTTP
/// response codes. If the code is 204, which has
/// multiple meaning, the mapping is handled looking on the request method
/// that has originated the response. The options are set thorugh the HTTP
/// headers and the option max-age, if not indicated, is set to the default
/// value (60 seconds). if the response has an enclosing entity, it is mapped
/// to a CoAP payload and the content-type of the CoAP message is set
/// properly.
/// </summary>
/// <param name="httpResponse">the http response</param>
/// <param name="coapRequest">the coap response</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="TranslationException"></exception>
public static Response GetCoapResponse(HttpWebResponse httpResponse, Request coapRequest)
{
if (httpResponse == null)
throw ThrowHelper.ArgumentNull("httpResponse");
if (coapRequest == null)
throw ThrowHelper.ArgumentNull("coapRequest");
HttpStatusCode httpCode = httpResponse.StatusCode;
StatusCode coapCode = 0;
// the code 204-"no content" should be managed
// separately because it can be mapped to different coap codes
// depending on the request that has originated the response
if (httpCode == HttpStatusCode.NoContent)
{
if (coapRequest.Method == Method.DELETE)
coapCode = StatusCode.Deleted;
else
coapCode = StatusCode.Changed;
}
else
{
if (!http2coapCode.TryGetValue(httpCode, out coapCode))
throw ThrowHelper.TranslationException("Cannot convert the HTTP status " + httpCode);
}
// create the coap reaponse
Response coapResponse = new Response(coapCode);
// translate the http headers in coap options
IEnumerable<Option> coapOptions = GetCoapOptions(httpResponse.Headers);
coapResponse.SetOptions(coapOptions);
// the response should indicate a max-age value (CoAP 10.1.1)
if (!coapResponse.HasOption(OptionType.MaxAge))
{
// The Max-Age Option for responses to POST, PUT or DELETE requests
// should always be set to 0 (draft-castellani-core-http-mapping).
coapResponse.MaxAge = coapRequest.Method == Method.GET ? CoapConstants.DefaultMaxAge : 0;
}
Byte[] buffer = new Byte[4096];
using (Stream ms = new MemoryStream(buffer.Length), dataStream = httpResponse.GetResponseStream())
{
Int32 read;
while ((read = dataStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
Byte[] payload = ((MemoryStream)ms).ToArray();
if (payload.Length > 0)
{
coapResponse.Payload = payload;
coapResponse.ContentType = GetCoapMediaType(httpResponse.GetResponseHeader("content-type"));
}
}
return coapResponse;
}