This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}) | |
{ |