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

Popular posts from this blog

قد رآی عینُ الله و ضرب یدُ الله

کیا عمران خان کی ماں قادیانی تھی ۔۔۔؟؟؟

اگر فاطمہ چوری کرتی تو کیا اس کا ہاتھ کاٹا جاتا؟ کیا یہ ممکن ہے؟ ("Agar (Meri Beti) Fatima (RaziAllahu Anha) Ne Bhi Chori Ki Hoti Tou Mai Uska Bhi Haath Kaat Leta”.)