Skip to content

Commit

Permalink
read the response body into a memory stream and then reset original s…
Browse files Browse the repository at this point in the history
…tream
  • Loading branch information
allenarthurgay committed Sep 21, 2012
1 parent 8bb4a3d commit 59c18dd
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs
Expand Up @@ -409,7 +409,7 @@ private void HandleResponseException<TResponse>(Exception ex, string requestUri)
{
using (var stream = errorResponse.GetResponseStream())
{
serviceEx.ResponseBody = stream.ToUtf8String();
serviceEx.ResponseBody = GetResponseBody(stream);
serviceEx.ResponseDto = DeserializeFromStream<TResponse>(stream);
}
}
Expand Down Expand Up @@ -440,7 +440,32 @@ private void HandleResponseException<TResponse>(Exception ex, string requestUri)
}
}

private WebRequest SendRequest(string requestUri, object request)
private static string GetResponseBody(Stream stream)
{
try
{
var memoryStream = new MemoryStream();
int readSize = 256;
byte[] buffer = new byte[readSize];
int count = stream.Read(buffer, 0, readSize);
while (count > 0)
{
memoryStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, readSize);
}
memoryStream.Position = 0;
stream.Position = 0;
return memoryStream.ToUtf8String();
}
catch (Exception e)
{
log.Error(e);
stream.Position = 0;
return "";
}
}

private WebRequest SendRequest(string requestUri, object request)
{
return SendRequest(HttpMethod ?? DefaultHttpMethod, requestUri, request);
}
Expand Down

0 comments on commit 59c18dd

Please sign in to comment.