示例#1
0
        private static async Task <Nancy.Response> ProcessReadMethod(NancyModule module, string outUri, IRequestProvider requestProvider)
        {
            var result = new Nancy.Response();
            HttpResponseMessage response = null;
            var responseContent          = string.Empty;

            try
            {
                var hc = new HttpClient()
                {
                    BaseAddress = new Uri(outUri)
                };

                hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var initialPath = InputModule.GetPath(module.Context.Request);
                response = await hc.GetAsync(
                    initialPath +
                    (initialPath.Contains('?') && initialPath.Contains('=') ? "&" : "?") +
                    "userid=" +
                    await InputModule.GetUserId(module, requestProvider)
                    );

                responseContent = await response.Content.ReadAsStringAsync();

                response.EnsureSuccessStatusCode();

                return(new TextResponse(HttpStatusCode.OK, responseContent));
            }
            catch (Exception ex)
            {
                result = new TextResponse(HttpStatusCode.BadRequest, (response != null) ? responseContent : ex.ToString());
            }
            return(result);
        }
示例#2
0
        private static async Task <Nancy.Response> ProcessWriteMethod(NancyModule module, HttpMethod method, string inUri, IRequestProvider requestProvider)
        {
            var result = new Nancy.Response();
            HttpResponseMessage response = null;
            var responseContent          = string.Empty;

            try
            {
                var hc = new HttpClient()
                {
                    BaseAddress = new Uri(inUri)
                };

                string  jsonString = RequestStream.FromStream(module.Request.Body).AsString();
                dynamic jsonObj    = string.IsNullOrEmpty(jsonString) ?
                                     new ExpandoObject() :
                                     JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

                jsonObj.UserId = await InputModule.GetUserId(module, requestProvider);

                var content = new StringContent(JsonConvert.SerializeObject(jsonObj));
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

                var message = new HttpRequestMessage(
                    method,
                    InputModule.GetPath(module.Context.Request)
                    )
                {
                    Content = content
                };
                foreach (var kvp in module.Context.Request.Headers.ToList())
                {
                    if (Array.IndexOf(new string[] { "Content-Length", "Content-Type" }, kvp.Key) < 0)
                    {
                        message.Headers.Add(kvp.Key, string.Join(',', kvp.Value));
                    }
                }
                response = await hc.SendAsync(message);

                responseContent = await response.Content.ReadAsStringAsync();

                response.EnsureSuccessStatusCode();

                result = new TextResponse(HttpStatusCode.OK, responseContent);
            }
            catch (Exception ex)
            {
                result = new TextResponse(HttpStatusCode.BadRequest, (response != null) ? responseContent : ex.ToString());
            }
            return(result);
        }