public static string ParseQuery(HTTP.Parameters parameters)
            {
                var queryParameters = ParametersParser.Parse(parameters);
                var queryArray      = queryParameters.Select(pair => string.Format("{0}={1}", pair.Key, pair.Value));

                return(string.Join("&", queryArray));
            }
示例#2
0
            public void Encode(System.Net.Http.HttpRequestMessage request, HTTP.Parameters parameters)
            {
                var json = JsonConvert.SerializeObject(parameters);

                request.Content = new StringContent(json);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
示例#3
0
        public Task <HTTP.Response> Perform(
            string URL,
            HTTP.Method method         = Method.GET,
            HTTP.Parameters parameters = null,
            HTTP.Headers headers       = null,
            HTTP.Encoder encoder       = null)
        {
            return(Task <HTTP.Response> .Run(async() =>
            {
                var client = new System.Net.Http.HttpClient();
                var request = this.Request(URL, method, parameters, headers, encoder);

                return new HTTP.Response(await client.SendAsync(request));
            }));
        }
示例#4
0
        private System.Net.Http.HttpRequestMessage Request(string URL,
                                                           HTTP.Method method,
                                                           HTTP.Parameters parameters,
                                                           HTTP.Headers headers,
                                                           HTTP.Encoder encoder)
        {
            var request = new HttpRequestMessage();

            request.Method     = method.Method();
            request.RequestUri = this.ApplyURL(URL);

            this.ApplyEncode(request, method, parameters, encoder);
            this.ApplyHeaders(request, headers);

            return(request);
        }
示例#5
0
            public void Encode(System.Net.Http.HttpRequestMessage request, HTTP.Parameters parameters)
            {
                if (request.Method == System.Net.Http.HttpMethod.Get)
                {
                    var builder     = new UriBuilder(request.RequestUri);
                    var queryString = HTTP.ParametersParser.ParseQuery(parameters);
                    builder.Query = queryString;

                    request.RequestUri = builder.Uri;
                }
                else
                {
                    var parsedParameters = HTTP.ParametersParser.Parse(parameters);
                    var content          = new FormUrlEncodedContent(parsedParameters);
                    request.Content = content;
                }
            }
示例#6
0
        private void ApplyEncode(System.Net.Http.HttpRequestMessage request,
                                 HTTP.Method method,
                                 HTTP.Parameters parameters,
                                 HTTP.Encoder encoder)
        {
            HTTP.Encoder requestEncoder = encoder ?? URLEncoder.Instance;

            if ((parameters?.Count ?? 0) != 0 &&
                method == Method.GET)
            {
                requestEncoder = URLEncoder.Instance;
            }

            if (requestEncoder != null &&
                (parameters?.Count ?? 0) != 0)
            {
                requestEncoder.Encode(request, parameters);
            }
        }
示例#7
0
            public void Encode(System.Net.Http.HttpRequestMessage request, HTTP.Parameters parameters)
            {
                if (request.Method == System.Net.Http.HttpMethod.Get)
                {
                    return;
                }

                var content = new MultipartFormDataContent();

                var parsedParameters = HTTP.ParametersParser.Parse(parameters);

                foreach (var pair in parsedParameters)
                {
                    var pairContent = new StringContent(pair.Value);
                    pairContent.Headers.ContentType = null;
                    content.Add(pairContent, pair.Key);
                }

                foreach (var file in this.files)
                {
                    if (file.content == null)
                    {
                        continue;
                    }
                    if (file.mimeType != null)
                    {
                        MediaTypeHeaderValue mediaType;
                        if (MediaTypeHeaderValue.TryParse(file.mimeType, out mediaType))
                        {
                            file.content.Headers.ContentType = mediaType;
                        }
                    }

                    content.Add(file.content, file.key, file.filename ?? "");
                }

                request.Content = content;
            }
            public static IEnumerable <KeyValuePair <string, string> > Parse(HTTP.Parameters parameters)
            {
                if (parameters == null)
                {
                    return(new KeyValuePair <string, string>[] { });
                }

                var parsedParameters = parameters.SelectMany(pair =>
                {
                    var parameterName  = pair.Key;
                    var parameterValue = pair.Value;
                    var parameterPairs = ParametersParser.ParseValue(parameterValue);
                    var resultList     = parameterPairs.Select(innerPair =>
                    {
                        var key    = string.Format("{0}{1}", parameterName, innerPair.Key);
                        var value  = innerPair.Value;
                        var result = new KeyValuePair <string, string>(key, value);
                        return(result);
                    });
                    return(resultList);
                });

                return(parsedParameters);
            }