Secure your Xamarin Application by Enhancing its HTTP Performance

HTTP-770x513

Though its a mobile application or a website which can be accessible on any operating system such as Windows, iPhone or Android, can perform awesomely on the cloud servers if it have a HTTP extension in its URL. With the constant use of the server nowadays the websites and applications are suffering several issues like slow network connection which directly proportional to the slow loading speed, losing connection, switching the data provider as from wi-fo to cellulat data or vice versa.

Do you know how you can improve the performance of your native Xamarin app using the HttpMessageHandlers?

Since for the invention of Xamarin.iOS in the IT world and the web app development industry the cross-platform app development technology Xamarin has update a new feature with which a app can be swap in a native HTTP platform, instead of using the mono network stack, Nsurlhandler use the Apple’s Nsurlsession API. It will permit you to create a transparency in exchanging or switching the web servers connection in a route to deliver the better performance activity in the xamarin mobile application.

In this deed, adding the below code in the system.net.httpclient’s will help you,

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
});

For the implementation of the code perfectly, you need to enable the neturlsession option in your project first. As the actual API from the .net background is still available in the app but is not support and responding the httpmessagehandler, so of you are also going through the similar issue, then the recreation of the code by syncing it with all the app features is the best option.

class MyAppClient
{
readonly HttpClient _httpClient = new HttpClient();

public async Task<string> GetChuck()
{
return await _httpClient.Get(“http://chucknorris.com”);
}
}

//Although “using” is good in general, don’t do this
using (var httpClient = new HttpClient())
{
return await _httpClient.Get(“http://chucknorris.com”);
}

Remember that, you can make multiple requests although you have just a single HTTPClient, hence, you do not need to create and dispose a new hhpclient every time. Also do not use the string in your direct read request for stream  Well, passing the json to string for the app or file downloading by passing the json to string is the most common mistake which the developers are mostly making. Thus, use the

JSON.NET function as jsonconcert.deserializeobject().
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))
{
return _serializer.Deserializer<YourClass>(json);
}

//If you need logging for development, use #if DEBUG and JsonConvert otherwise
#if DEBUG
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
{
string text = reader.ReadToEnd();
Debug.WriteLine(“RECEIVED: ” + text);
return JsonConvert.DeserializeObject<YourClass>(text);
}
#else
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
return _serializer.Deserializer<YourClass>(json);
}

by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>