public static ErrorResponseException FromResponseMessage(HttpResponseMessage response, bool readErrorString = true) { var sb = new StringBuilder("Status code: ").Append(response.StatusCode).AppendLine(); string responseString = null; if (readErrorString && response.Content != null) { var readAsStringAsync = response.GetResponseStreamWithHttpDecompression(); if (readAsStringAsync.IsCompleted) { using (var streamReader = new StreamReader(readAsStringAsync.Result)) { responseString = streamReader.ReadToEnd(); sb.AppendLine(responseString); } } } return new ErrorResponseException(response, sb.ToString()) { ResponseString = responseString }; }
public async Task<byte[]> ReadResponseBytesAsync() { Response = await httpClient.SendAsync(new HttpRequestMessage(method, url)) .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(url); if (Response.IsSuccessStatusCode == false) throw new ErrorResponseException(Response); // TODO: Use RetryIfNeedTo(task, ReadResponseBytesAsync) return ConvertStreamToBytes(await Response.GetResponseStreamWithHttpDecompression()); }