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


C# Request.SetOptions方法代码示例

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


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

示例1: GetCoapRequest

        /// <summary>
        /// Gets the coap request. Creates the CoAP request from the HTTP method and
	    /// mapping it through the properties file. The uri is translated using
	    /// regular expressions, the uri format expected is either the embedded
	    /// mapping (http://proxyname.domain:80/proxy/coapserver:5683/resource
	    /// converted in coap://coapserver:5683/resource) or the standard uri to
	    /// indicate a local request not to be forwarded. The method uses a decoder
	    /// to translate the application/x-www-form-urlencoded format of the uri. The
	    /// CoAP options are set translating the headers. If the HTTP message has an
	    /// enclosing entity, it is converted to create the payload of the CoAP
	    /// message; finally the content-type is set accordingly to the header and to
        /// the entity type.
        /// </summary>
        /// <param name="httpRequest">the http request</param>
        /// <param name="proxyResource"></param>
        /// <param name="proxyingEnabled"></param>
        /// <returns></returns>
        public static Request GetCoapRequest(IHttpRequest httpRequest, String proxyResource, Boolean proxyingEnabled)
        {
            if (httpRequest == null)
                throw ThrowHelper.ArgumentNull("httpRequest");
            if (proxyResource == null)
                throw ThrowHelper.ArgumentNull("proxyResource");

            Method coapMethod;
            if (!http2coapMethod.TryGetValue(httpRequest.Method, out coapMethod))
                throw ThrowHelper.TranslationException(httpRequest.Method + " method not mapped");

            // create the request
            Request coapRequest = new Request(coapMethod);

            // get the uri
            String uriString = httpRequest.RequestUri;
            // remove the initial "/"
            if (uriString.StartsWith("/"))
                uriString = uriString.Substring(1);
            // TODO URLDecode

            // if the uri contains the proxy resource name, the request should be
            // forwarded and it is needed to get the real requested coap server's
            // uri
            // e.g.:
            // /proxy/[::1]:5684/helloWorld
            // proxy resource: /proxy
            // coap server: [::1]:5684
            // coap resource: helloWorld
            Regex regex = new Regex(".?" + proxyResource + ".*");
            if (regex.IsMatch(uriString))
            {
                // find the first occurrence of the proxy resource
                Int32 index = uriString.IndexOf(proxyResource);
                // delete the slash
                index = uriString.IndexOf('/', index);
                uriString = uriString.Substring(index + 1);

                if (proxyingEnabled)
                {
                    // if the uri hasn't the indication of the scheme, add it
                    if (!uriString.StartsWith("coap://") &&
                        !uriString.StartsWith("coaps://"))
                    {
                        uriString = "coap://" + uriString;
                    }

                    // the uri will be set as a proxy-uri option
                    // set the proxy-uri option to allow the lower layers to underes
                    coapRequest.SetOption(Option.Create(OptionType.ProxyUri, uriString));
                }
                else
                {
                    coapRequest.URI = new Uri(uriString);
                }

                // TODO set the proxy as the sender to receive the response correctly
                //coapRequest.PeerAddress = new EndpointAddress(IPAddress.Loopback);
            }
            else
            {
                // if the uri does not contains the proxy resource, it means the
                // request is local to the proxy and it shouldn't be forwarded

                // set the uri string as uri-path option
                coapRequest.UriPath = uriString;
            }

            // translate the http headers in coap options
            IEnumerable<Option> coapOptions = GetCoapOptions(httpRequest.Headers);
            coapRequest.SetOptions(coapOptions);

            // the payload
            if (httpRequest.InputStream != null)
            {
                Byte[] tmp = new Byte[4096];
                MemoryStream ms = new MemoryStream(tmp.Length);
                Int32 read;
                while ((read = httpRequest.InputStream.Read(tmp, 0, tmp.Length)) > 0)
                {
                    ms.Write(tmp, 0, read);
                }
                coapRequest.Payload = ms.ToArray();
//.........这里部分代码省略.........
开发者ID:vjine,项目名称:CoAP.NET,代码行数:101,代码来源:HttpTranslator.cs


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