Skip to content

Instantly share code, notes, and snippets.

View jonathanpeppers's full-sized avatar
🌶️
spicy!

Jonathan Peppers jonathanpeppers

🌶️
spicy!
View GitHub Profile
@jonathanpeppers
jonathanpeppers / http.cs
Created February 3, 2017 20:23
HttpClient Example
var httpClient = new HttpClient();
string text = await httpClient.GetStringAsync("http://chucknorris.com/");
//NOTE: you can't do this, or you will not get an automatic NSUrlSessionHandler
var httpClient = new HttpClient(new HttpClientHandler
{
//Some settings
});
@jonathanpeppers
jonathanpeppers / MyAppClient.cs
Last active February 4, 2017 05:23
How to do HttpClient
//Use this class as a singleton in an IoC container
class MyAppClient
{
readonly HttpClient _httpClient = new HttpClient();
public async Task<string> GetChuck()
{
return await _httpClient.Get("http://chucknorris.com");
}
}
@jonathanpeppers
jonathanpeppers / gzip.cs
Last active July 29, 2021 13:50
HttpClient with GZip
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
//Nope, you don't have to even mess with GZipStream
string text = await httpClient.GetAsString("http://chucknorris.com");
//NOTE: but if you need this to work on Windows or non-Xamarin platforms, you will need this ctor for HttpClient
var httpClient = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip,
@jonathanpeppers
jonathanpeppers / json.cs
Last active February 4, 2017 05:25
JSON from Stream
//reuse this
private JsonSerializer _serializer = new JsonSerializer();
var response = await _httpClient.GetAsync("http://chucknorris.com/api/dropkick");
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
@jonathanpeppers
jonathanpeppers / JsonContent.cs
Last active February 4, 2017 06:19
JsonContent for writing gzipped JSON in web requests
public class JsonContent : HttpContent
{
private readonly JsonSerializer _serializer = new JsonSerializer();
private readonly object _value;
public JsonContent(object value)
{
_value = value;
Headers.ContentType = new MediaTypeHeaderValue("application/gzip");
}
@jonathanpeppers
jonathanpeppers / GzipHandler.cs
Last active June 8, 2022 05:39
DelegatingHandler for GZip in ASP.NET Web API
public class GzipHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content != null &&
request.Content.Headers.ContentType != null &&
(request.Content.Headers.ContentType.MediaType == "application/gzip" ||
request.Content.Headers.ContentType.MediaType == "application/deflate"))
{
string encodingType = request.Content.Headers.ContentType.MediaType.Split('/').Last();
@jonathanpeppers
jonathanpeppers / SignalR.cs
Created February 4, 2017 05:53
SignalR with Xamarin NSUrlSessionHandler
var connection = new HubConnection("http://yourserver.com/signalr");
await connection.Start(new ServerSentEventsTransport(new NativeHttpClient()));
//And NativeHttpClient
public class NativeHttpClient : DefaultHttpClient
{
protected override HttpMessageHandler CreateHandler()
{
return new NSUrlSessionHandler();
}
@jonathanpeppers
jonathanpeppers / NativeHttpClient.cs
Last active February 4, 2017 06:00
Custom IHttpClient for SignalR
public class NativeHttpClient : IHttpClient
{
private HttpClient _client;
private IConnection _connection;
public void Initialize(IConnection connection)
{
_connection = connection;
_client = new HttpClient();
_client.Timeout = TimeSpan.FromMilliseconds(-1); //NOTE: infinite timeout because upper SignalR layer handles timeouts
@jonathanpeppers
jonathanpeppers / PurchaseService.cs
Last active February 16, 2017 18:46
Abstract PurchaseService for IAPs
//NOTE: to be used as a singleton in your app
public abstract class PurchaseService
{
/// <summary>
/// Retrieves the prices from iTunes or Google Play
/// </summary>
public abstract Task<Purchase[]> GetPrices(params string[] ids);
/// <summary>
/// Buys an in-app purchase
@jonathanpeppers
jonathanpeppers / ApplePurchaseService.cs
Created February 16, 2017 18:50
ApplePurchaseService - GetPrices
public class ApplePurchaseService : PurchaseService
{
public async override Task<Purchase[]> GetPrices(params string[] ids)
{
using (var del = new RequestDelegate())
using (var request = new SKProductsRequest(new NSSet(ids))
{
Delegate = del,
})
{