Ejemplo n.º 1
0
        public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
        {
            Dictionary <string, string>        templateParameters = new Dictionary <string, string>();
            List <KeyValuePair <int, object> > bodyParameters     = new List <KeyValuePair <int, object> >();

            for (int i = 0; i < parameters.Length; i++)
            {
                if (pathMapping.ContainsKey(i))
                {
                    templateParameters.Add(pathMapping[i], parameters[i] as string);
                }
                else if (queryMapping.ContainsKey(i))
                {
                    if (parameters[i] != null)
                    {
                        templateParameters.Add(queryMapping[i].Value, queryStringConverter.ConvertValueToString(parameters[i], queryMapping[i].Key));
                    }
                }
                else
                {
                    bodyParameters.Add(new KeyValuePair <int, object>(i, parameters[i]));
                }
            }

            HttpRequestMessageProperty httpRequest = new HttpRequestMessageProperty();

            httpRequest.Method = this.method;
            httpRequest.Headers[System.Net.HttpRequestHeader.ContentType] = "application/json";
            Message result;

            if (this.method == HttpMethodGet || this.method == HttpMethodHead)
            {
                httpRequest.SuppressEntityBody = true;
                result = Message.CreateMessage(MessageVersion.None, null);
            }
            else
            {
                byte[] body = GetJsonBody(bodyParameters);
                result = Message.CreateMessage(MessageVersion.None, null, new RawBodyWriter(body));
            }

            result.Properties.Add(HttpRequestMessageProperty.Name, httpRequest);
            result.Headers.To = uriTemplate.BindByName(baseUri, templateParameters);

            result.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initiates a GET request for the given operation and return the server respose (as a task).
        /// </summary>
        /// <param name="result">The result object which contains information about which operation was performed.</param>
        /// <param name="parameters">The parameters to the server method, or <c>null</c> if no parameters.</param>
        /// <param name="queryOptions">The query options if any.</param>
        /// <returns></returns>
        private Task <HttpResponseMessage> GetAsync(string operationName, IDictionary <string, object> parameters, IList <ServiceQueryPart> queryOptions, CancellationToken cancellationToken)
        {
            int i          = 0;
            var uriBuilder = new StringBuilder();

            uriBuilder.Append(operationName);

            // Parameters
            if (parameters != null && parameters.Count > 0)
            {
                var converter = new QueryStringConverter();
                foreach (var param in parameters)
                {
                    if (param.Value != null)
                    {
                        uriBuilder.Append(i++ == 0 ? '?' : '&');
                        uriBuilder.Append(Uri.EscapeDataString(param.Key));
                        uriBuilder.Append("=");

                        var value = converter.ConvertValueToString(param.Value, param.Value.GetType());
                        uriBuilder.Append(Uri.EscapeDataString(value));
                    }
                }
            }

            // Query options
            if (queryOptions != null && queryOptions.Count > 0)
            {
                foreach (var queryPart in queryOptions)
                {
                    uriBuilder.Append(i++ == 0 ? "?$" : "&$");
                    uriBuilder.Append(queryPart.QueryOperator);
                    uriBuilder.Append("=");
                    uriBuilder.Append(Uri.EscapeDataString(queryPart.Expression));
                }
            }

            // TODO: Switch to POST if uri becomes to long, we can do so by returning nul ...l
            var uri = uriBuilder.ToString();

            return(HttpClient.GetAsync(uri, cancellationToken));
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            // <Snippet0>
            // <Snippet1>
            QueryStringConverter converter = new QueryStringConverter();

            // </Snippet1>
            // <Snippet2>
            if (converter.CanConvert(typeof(Int32)))
            {
                converter.ConvertStringToValue("123", typeof(Int32));
            }
            // </Snippet2>
            // <Snippet3>
            int    value    = 321;
            string strValue = converter.ConvertValueToString(value, typeof(Int32));

            Console.WriteLine("the value = {0}, the string representation of the value = {1}", value, strValue);
            // </Snippet3>
            // </Snippet0>
        }