I couldn't figure out how to do this with the provided framework tools so I had to roll my own. First is the method that does the actual request that shows how to specify the header that requests gzip encoding and unzips the result.
private static IEnumerable<dynamic> MakeHttpQuery(string uri)
{
var request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");
try
{
var response = request.GetResponse();
var contentEncoding = response.Headers[HttpResponseHeader.ContentEncoding];
var responseStream = response.GetResponseStream();
if (!string.IsNullOrEmpty(contentEncoding) && contentEncoding.Equals("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
var json = JsonObject.Parse(responseStream);
var d = json["d"];
if (!d.IsArray) return new JsonArray(new[] {d}).Values;
else return ((JsonArray) d).Values;
}
catch (WebException webException)
{
log4net.LogManager.GetLogger(typeof(ProfileMediaDataInterface)).Error(webException);
return new JsonArray(new JsonValue[] {}).Values;
}
}
The DynamicJson library is an open-source library that I wrote a while ago which came in very handy here. You can very easily leave out the Accept: application/json header and then you will get back XML. In this case, something like linq-to-xml will also work, in very much the same way.
Next the client code. This shows how to construct the URL to pass to the MakeHttpQuery method also what to do with the result.
public static List<BenchmarkListSqr> GetBenchmarkListSqr(string currencyCode)
{
return
MakeHttpQuery(
CreateDataService()
.BenchmarkList
.Where(bm => bm.Currency == currencyCode)
.ToString())
.Select(x => new BenchmarkListSqr(
x.Currency,
x.AssetClass,
ToNullDateTime(x.AvailableFromDate),
x.ID,
ToNullDateTime(x.InceptionDate),
x.Name,
x.Region,
ToNullDecimal(x.ShareID)))
.ToList();
}