/// <summary> /// Process the web response and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // get the response encoding Encoding encoding = ContentHelper.GetEncoding(response); string str = StreamHelper.DecodeStream(responseStream, encoding); RestReturnType returnType = CheckReturnType(response); Object obj = ConvertOutput(str, returnType); WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } } }
/// <summary> /// Process the web reponse and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overriden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // determine the response type RestReturnType returnType = CheckReturnType(response); // get the response encoding Encoding encoding = ContentHelper.GetEncoding(response); object obj = null; Exception ex = null; string str = StreamHelper.DecodeStream(responseStream, encoding); bool convertSuccess = false; // On CoreCLR, we need to explicity load Json.NET JsonObject.ImportJsonDotNetModule(this); if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string obj = str; } WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } } }
internal override void ProcessResponse(WebResponse response) { if (response == null) { throw new ArgumentNullException("response"); } MemoryStream stream = new MemoryStream(); using (BufferingStreamReader reader = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (base.ShouldWriteToPipeline && !this.TryProcessFeedStream(reader)) { RestReturnType type = this.CheckReturnType(response); stream = StreamHelper.ReadStream(reader, response.ContentLength, this); Encoding encoding = ContentHelper.GetEncoding(response); object obj2 = null; Exception exRef = null; string json = StreamHelper.DecodeStream(stream, encoding); bool flag = false; if (type == RestReturnType.Json) { flag = this.TryConvertToJson(json, out obj2, ref exRef) || this.TryConvertToXml(json, out obj2, ref exRef); } else { flag = this.TryConvertToXml(json, out obj2, ref exRef) || this.TryConvertToJson(json, out obj2, ref exRef); } if (!flag) { obj2 = json; } base.WriteObject(obj2); } if (base.ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(StreamHelper.ReadStream(reader, response.ContentLength, this), base.QualifiedOutFile, this); } } }
internal static string DecodeStream(MemoryStream stream, string characterSet) { Encoding encoding = ContentHelper.GetEncoding(characterSet); return(DecodeStream(stream, encoding)); }