Parse() публичный статический Метод

Parses a given string as a HeaderCollection.
public static Parse ( string headers ) : HeaderCollection
headers string String to be parsed.
Результат HeaderCollection
Пример #1
0
        /// <summary>Parses a given string as a <see cref="RequestInfo" />.</summary>
        /// <param name="method">Method of the request.</param>
        /// <param name="url">URL of the request.</param>
        /// <param name="message">Request content.</param>
        /// <returns>Instance of the <see cref="RequestInfo" />.</returns>
        public static RequestInfo Parse(Verb method, HttpUrl url, string message)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            if (!url.IsAbsolute)
            {
                throw new ArgumentOutOfRangeException("url");
            }

            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (message.Length == 0)
            {
                throw new ArgumentOutOfRangeException("message");
            }

            string[]         parts    = Regex.Split(message, "\r\n\r\n");
            Encoding         encoding = Encoding.UTF8;
            HeaderCollection headers  = HeaderCollection.Parse(parts[0]);

            return(new RequestInfo(method, url, (parts.Length > 1 ? new MemoryStream(encoding.GetBytes(parts[1].Trim('\r', '\n'))) : new MemoryStream()), new BasicClaimBasedIdentity(), headers));
        }
Пример #2
0
        /// <summary>Calls the ReST service using specified HTTP verb.</summary>
        /// <param name="verb">The HTTP verb.</param>
        /// <param name="url">Relative URL of the service to call.</param>
        /// <param name="accept">Enumeration of accepted media types.</param>
        /// <param name="contentType">Enumeration of possible content type media types.</param>
        /// <param name="responseType">Type of the response.</param>
        /// <param name="uriArguments">The URI template arguments.</param>
        /// <param name="bodyArguments">The body arguments.</param>
        /// <returns>Result of the call.</returns>
        protected object Call(Verb verb, string url, IEnumerable <string> accept, IEnumerable <string> contentType, Type responseType, IDictionary <string, object> uriArguments, params object[] bodyArguments)
        {
            var callUrl     = BuildUrl(url, uriArguments);
            var validAccept = (!accept.Any() ? _converterProvider.SupportedMediaTypes :
                               _converterProvider.SupportedMediaTypes.Join(accept, outer => outer, inner => inner, (outer, inner) => inner));
            var        accepted = (validAccept.Any() ? validAccept : new[] { "*/*" });
            WebRequest request  = _webRequestProvider.CreateRequest((Uri)callUrl, new Dictionary <string, string>()
            {
                { Header.Accept, String.Join(", ", accepted) }
            });

            if ((!String.IsNullOrEmpty(CredentialCache.DefaultNetworkCredentials.UserName)) && (!String.IsNullOrEmpty(CredentialCache.DefaultNetworkCredentials.Password)))
            {
                var credentials = new CredentialCache();
                credentials.Add(new Uri(callUrl.Authority), _authenticationScheme, new NetworkCredential(CredentialCache.DefaultNetworkCredentials.UserName, CredentialCache.DefaultNetworkCredentials.Password));
                request.Credentials     = credentials;
                request.PreAuthenticate = true;
            }

            request.Method = verb.ToString();
            if ((bodyArguments != null) && (bodyArguments.Length > 0))
            {
                FillRequestBody(verb, callUrl, request, contentType, accepted, bodyArguments);
            }

            var response = (HttpWebResponse)request.GetResponse();

            ParseContentRange(response, uriArguments);
            if (responseType == null)
            {
                return(null);
            }

            RequestInfo fakeRequest = new RequestInfo(verb, callUrl, response.GetResponseStream(), new BasicClaimBasedIdentity(), HeaderCollection.Parse(response.Headers.ToString()));
            var         result      = _resultBinder.BindResults(responseType, fakeRequest);

            return(result.FirstOrDefault(responseType.IsInstanceOfType));
        }