Skip to content
This repository was archived by the owner on Apr 10, 2018. It is now read-only.

Commit e78a160

Browse files
committed
Merge pull request #65 from VQComms/restresponsecontent
Added Content property to RestResponse
2 parents 84098fa + 99e3110 commit e78a160

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

RestSharp.Portable.Core/IRestResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@ public interface IRestResponse
4848
/// Gets the description for the HTTP status code
4949
/// </summary>
5050
string StatusDescription { get; }
51+
52+
/// <summary>
53+
/// String representation of response content
54+
/// </summary>
55+
string Content {get;}
5156
}
5257
}

RestSharp.Portable.Core/RestResponse.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace RestSharp.Portable
1010
/// </summary>
1111
public class RestResponse : IRestResponse
1212
{
13+
private string content;
14+
1315
/// <summary>
1416
/// Initializes a new instance of the <see cref="RestResponse" /> class.
1517
/// </summary>
@@ -61,6 +63,15 @@ protected RestResponse(IRestClient client, IRestRequest request)
6163
/// </summary>
6264
public string StatusDescription { get; private set; }
6365

66+
/// <summary>
67+
/// String representation of response content
68+
/// </summary>
69+
public string Content
70+
{
71+
get { return this.content ?? (this.content = this.RawBytes.AsString()); }
72+
set { this.content = value; }
73+
}
74+
6475
/// <summary>
6576
/// Gets the REST client that created this response
6677
/// </summary>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Text;
2+
using System.IO;
3+
4+
namespace RestSharp.Portable
5+
{
6+
/// <summary>
7+
/// Extensions for <see cref="RestResponse"/>
8+
/// </summary>
9+
public static class RestResponseExtensions
10+
{
11+
/// <summary>
12+
/// Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
13+
/// http://www.shrinkrays.net/code-snippets/csharp/an-extension-method-for-converting-a-byte-array-to-a-string.aspx
14+
/// </summary>
15+
/// <param name="buffer">An array of bytes to convert</param>
16+
/// <returns>The byte as a string.</returns>
17+
public static string AsString(this byte[] buffer)
18+
{
19+
if (buffer == null)
20+
{
21+
return "";
22+
}
23+
24+
// Ansi as default
25+
Encoding encoding = Encoding.UTF8;
26+
27+
#if !PCL
28+
return encoding.GetString(buffer, 0, buffer.Length);
29+
#else
30+
if (buffer.Length == 0)
31+
return "";
32+
33+
/*
34+
EF BB BF UTF-8
35+
FF FE UTF-16 little endian
36+
FE FF UTF-16 big endian
37+
FF FE 00 00 UTF-32, little endian
38+
00 00 FE FF UTF-32, big-endian
39+
*/
40+
41+
if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
42+
{
43+
encoding = Encoding.UTF8;
44+
}
45+
else if (buffer[0] == 0xfe && buffer[1] == 0xff)
46+
{
47+
encoding = Encoding.Unicode;
48+
}
49+
else if (buffer[0] == 0xfe && buffer[1] == 0xff)
50+
{
51+
encoding = Encoding.BigEndianUnicode; // utf-16be
52+
}
53+
54+
using (MemoryStream stream = new MemoryStream())
55+
{
56+
stream.Write(buffer, 0, buffer.Length);
57+
stream.Seek(0, SeekOrigin.Begin);
58+
59+
using (StreamReader reader = new StreamReader(stream, encoding))
60+
{
61+
return reader.ReadToEnd();
62+
}
63+
}
64+
#endif
65+
}
66+
}
67+
}
68+

RestSharp.Portable.Core/RestSharp.Portable.Core.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@
6464
<Compile Include="$(MSBuildThisFileDirectory)UrlEscapeFlags.cs" />
6565
<Compile Include="$(MSBuildThisFileDirectory)UrlEscapeUtility.cs" />
6666
<Compile Include="$(MSBuildThisFileDirectory)UrlUtility.cs" />
67+
<Compile Include="$(MSBuildThisFileDirectory)RestResponseExtensions.cs" />
6768
</ItemGroup>
6869
</Project>

0 commit comments

Comments
 (0)