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); }
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(); }
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; }