C#: Call Rest API through C#
private void Calling()
{
GetReq("http://domain.com/api/Customer/GetAll/?CustomerID=001&FirstName=D");
}
public static async Task<string> GetReq(string url)
{
string message = null;
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage responseMessage = await client.GetAsync(url);
message = await responseMessage.Content.ReadAsStringAsync();
if (responseMessage.IsSuccessStatusCode)
{
return message;
}
else
{
return message + " " + responseMessage.StatusCode.ToString() + " " + responseMessage.ToString();
}
}
catch (Exception ex)
{
//Debug.WriteLine(ex);
return ("Error while connecting device to network. \n" + ex.ToString());
}
}
}
Comments
Post a Comment