Tag Archives: Web Service

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...";
			}
			else
			{
				return content;
			}
		}
	}
	else
	{
		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";
	else
		request.ContentType = "application/x-www-form-urlencoded";

	request.Method = "POST";
	var stream = await request.GetRequestStreamAsync();
	using (var writer = new StreamWriter(stream))
	{
		writer.Write(serializedDataString);
		writer.Flush();
		writer.Dispose();
	}

	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… ^_^

CHEERS !

Save Sinhala Unicode Characters in MySQL Database from C#

So recently I came across this situation where I had to save some Sinhala Unicode text from a ASP.net Web Service to a MySQL Database. But when I executed the program after coding the usual INSERT statements, the text had been saved as a set of question marks (??????) in the database column, which had data type set as VARCHAR. I knew that this is due to the fact that MySQL was unable to recognize the Unicode Stream passed by the C# web service. And finally I came up with a Solution. So here is what you have to do if you ever come across such a situation,

  • Set the database Column data type to VARCHAR or TEXT -type
  • Set the COLLATION type of the column to – utf8_unicode_ci
  • Add the following statement at the end of the Database Connection String, “Charset=utf8”, which is to enable MySQL to recognize the data stream.
  • And we are done !

TADAAAAAAAAAAAAA ! 😀

Now you must be able to save any kind of Sinhala Unicode in your MySQL database from C# application…