Exemplo n.º 1
0
        private static RestResponse CreateNormalResponse(HttpWebResponse webResponse)
        {
            string   content;
            Encoding encodingToUse = Encoding.UTF8;

            if (!string.IsNullOrEmpty(webResponse.ContentEncoding))
            {
                encodingToUse = Encoding.GetEncoding(webResponse.ContentEncoding);
            }
            else if (!string.IsNullOrEmpty(webResponse.Headers[HttpResponseHeader.ContentType]))
            {
                encodingToUse = RestContentTypes.ParseContentTypeHeader(webResponse.Headers[HttpResponseHeader.ContentType]).Encoding;
            }


            using (Stream s = webResponse.GetResponseStream())
                using (StreamReader sr = new StreamReader(s, encodingToUse))
                {
                    content = sr.ReadToEnd();
                }

            RestResponse response = new RestResponse(
                webResponse.StatusCode,
                webResponse.StatusDescription,
                webResponse.Headers,
                content
                );

            webResponse.Close();
            return(response);
        }
Exemplo n.º 2
0
        public void AddParameter(string name, object value)
        {
            if (!string.IsNullOrEmpty(_body))
            {
                throw new NotSupportedException("Cannot have a body and parameters");
            }

            if (ParamsType == RequestParamsType.Body)
            {
                Headers["Content-Type"] = RestContentTypes.GetContentTypeHeader(RestContentTypes.FormUrlEncoded, Encoding);
            }

            Parameters[name] = value.ToString();
        }
Exemplo n.º 3
0
        public void AddBody(string content, string contentType, Encoding encoding = null)
        {
            if (ParamsType != RequestParamsType.Body)
            {
                throw new NotSupportedException($"{Method} cannot have a body");
            }
            else if (Parameters.Count > 0)
            {
                throw new NotSupportedException("Cannot have a body and parameters");
            }

            if (encoding != null)
            {
                Encoding = encoding;
            }

            Headers["Content-Type"] = RestContentTypes.GetContentTypeHeader(contentType, Encoding);
            _body = content;
        }