How to make POST & GET calls to an ASP.NET RESTful Web Service in Xamarin.Forms

So you are a Xamarin Forms developer ? Well I’m pretty sure this may come in much handy to you… 🙂

Ever wanted to call an ASP.NET RESTful Web Service method from your Xamarin.Forms project, specially when you have a Xamarin.Forms PCL Project ? Well heck yeah I did… 😉 So here is the code I used which I hope it might come in handy do you as well.

Extra Ingredients you may need –

Json.NET Library – For Serializing and Deserializing  data

Microsoft Async Library – For making Async web calls from your PCL project

Make sure you install those libraries (use Nuget Package Manager would be easier) in your Xamarin.Forms Solution (all four of projects – Portable (PCL), Android, iOS and WinPhone). And you will be good to go… 😉

GET Request Method..

Below is the GET request method you could use to call any GET Methods in your ASP.NET RESTful web service.

public async Task<string> MakeGetRequest(string url)
	var request = HttpWebRequest.Create(url);

	HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;

	if (response.StatusCode == HttpStatusCode.OK)
		using (StreamReader reader = new StreamReader(response.GetResponseStream()))
			var content = reader.ReadToEnd();
			if (string.IsNullOrWhiteSpace(content))
				return response.StatusCode + "Response contained an empty body...";
				return content;
		return response.StatusCode.ToString();


Now if you need to deserialize any response you get from the above method call, then you may use the following simple method.. 😉

Deserialize any response from the Web Method call…
public object DeserializeContent<T>(String stringToDeserialize)
	DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<T>));
	MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(stringToDeserialize));
	return (List<T>)ser.ReadObject(stream);
POST Request Method..

Use this method to make any POST Method calls to your ASP.NET RESTful web service.

public async Task<string> MakePostRequest(string url, string serializedDataString, bool isJson = true)
	HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
	if (isJson)
		request.ContentType = "application/json";
		request.ContentType = "application/x-www-form-urlencoded";

	request.Method = "POST";
	var stream = await request.GetRequestStreamAsync();
	using (var writer = new StreamWriter(stream))

	var response = await request.GetResponseAsync();
	var respStream = response.GetResponseStream();

	using (StreamReader sr = new StreamReader(respStream))
		return sr.ReadToEnd();


Now if you are in need of Serializing any data that is needed to be passed into the above POST method call, you can use the following simple method which is written using JSON.NET,

Serialize any data to be passed through the POST Web method call..
public String SerializeContent(Object objectToSerialize) 
	return JsonConvert.SerializeObject(objectToSerialize);


TADAAAA ! 😀 There you go, hope it was helpful for any of you awesome Xamarin Developers… ^_^



3 thoughts on “How to make POST & GET calls to an ASP.NET RESTful Web Service in Xamarin.Forms

  1. You are a life saver! Thank you for this. I was using someone’s extensions for GetResponse and GetRequestStream in PCL and many times the code would just hang at evt.WaitOne(). In debug mode, this has an extremely long timeout and the request object has no method to set a timeout. I have been looking for an alternative and this is just what I needed!

    Thank you again!

  2. Thank you so much for saved my time….very usefull. How about in Xamarin android may i use the above code samples.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.