Exemplo n.º 1
0
        private CradleResponse GetResponse(HttpWebResponse webResponse)
        {
            var responseData = webResponse.GetResponseStream().ReadToEnd();
            var content = Encoding.UTF8.GetString(responseData, 0, responseData.Length);

            var response = new CradleResponse
            {
                Uri = webResponse.ResponseUri,
                Data = content,
                Headers = GetHeaders(webResponse.Headers),
                StatusCode = webResponse.StatusCode,
                StatusDescription = webResponse.StatusDescription,
                Method = _httpVerb
            };
            return response;
        }
Exemplo n.º 2
0
        private CradleResponse RawRequest(string method, string path, IDictionary<string, string> urlParams, string data = null)
        {
            path = (string.IsNullOrWhiteSpace(path) ? "/" : path).Replace(new Regex(@"/https?:\/\//"), "").Replace(new Regex(@"/\/{2,}/"), "");
            if (path[0] != '/')
            {
                path = '/' + path;
            }

            var requestUri = BuildRequestUri(path, urlParams);

            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = method.ToUpper();
            request.UserAgent = string.Format(CultureInfo.InvariantCulture, "DotCradle/{0}",
                                                  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
            if (!string.IsNullOrWhiteSpace(data))
            {
                var byteArray = Encoding.UTF8.GetBytes(data);
                var dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                request.ContentLength = byteArray.Length;
                request.ContentType = "application/json";
            }
            else
            {
                request.ContentLength = 0;
            }

            var webResponse = (HttpWebResponse)request.GetResponse();
            var responseData = webResponse.GetResponseStream().ReadToEnd();
            var content = Encoding.UTF8.GetString(responseData, 0, responseData.Length);

            var response = new CradleResponse
                {
                    Data = content,
                    Headers = GetHeaders(webResponse.Headers),
                    StatusCode = webResponse.StatusCode
                };
            return response;
        }