Tuesday, June 4, 2013

Call Rest Service from C#


There are lots of ways to call REST Services in your applcation, you can either use Javascript (JQuery Functions) or C# . here i am not going to compare anything. If you want to know how to call using JQuery go here. in this post we are going to look how to call REST Services using C#

Invoking Rest Services from C# Application

public class ServiceConnector
    {
        string EndPoint ;
        string Method ; 
        string ContentType ;
        EventLogger logger ;

        public ServiceConnector(string endpoint,string method, string contenttype,string logName)
        {
            this.EndPoint = endpoint;
            this.Method = method;
            this.ContentType = contenttype;
            logger = new EventLogger(logName);
        }
        public string MakeRestRequest(string parameters, string PostData)
        {
            var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
            logger.WriteToEventLog("Calling Services...");
            request.Method = Method;
            request.ContentLength = 0;
            request.ContentType = ContentType;

            if (!string.IsNullOrEmpty(PostData) && Method == "POST")
            {
                var encoding = new UTF8Encoding();
                var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }
           
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                    logger.WriteToEventLog("Web Service Call Failed. More Details : " + message);
                    throw new ApplicationException(message);
                }

                // grab the response
                using (var responseStream = response.GetResponseStream())
                {  
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                    logger.WriteToEventLog("Web Service Call succeed, response value is " + responseValue);
                }

                return responseValue;
            }
        }
    }

Check whether Service call work fines or not by creating console application.
use this code to call that function


ServiceConnector Service = new ServiceConnector("Enter your Service EndPoint here", "Enter Method", "Enter ContentType", "MyServiceLog");
String response = Service.MakeRestRequest("Pass your parameters", "Pass your PostData, keep it as empty if you do GET");



3 comments:

Unknown said...

north face, hogan, coach outlet, air force, hollister pas cher, michael kors, coach outlet, lululemon, burberry, sac guess, nike roshe, nike free, true religion jeans, air jordan pas cher, lacoste pas cher, ralph lauren pas cher, michael kors, kate spade outlet, ralph lauren uk, michael kors, new balance pas cher, true religion jeans, ray ban uk, nike roshe run, converse pas cher, michael kors, hermes, mulberry, vans pas cher, louboutin pas cher, true religion outlet, ray ban pas cher, nike air max, oakley pas cher, air max, timberland, nike free run uk, kate spade handbags, nike air max, hollister, tn pas cher, replica handbags, nike air max, vanessa bruno, abercrombie and fitch, sac longchamp, true religion jeans, nike blazer, coach purses, longchamp pas cher, north face

Unknown said...

ugg,uggs,uggs canada, canada goose, ugg,ugg australia,ugg italia, canada goose, hollister, wedding dresses, moncler, louis vuitton, canada goose outlet, swarovski, pandora charms, pandora jewelry, moncler outlet, moncler, sac louis vuitton pas cher, replica watches, louis vuitton, marc jacobs, louis vuitton, vans, moncler, swarovski crystal, pandora jewelry, toms shoes, moncler, juicy couture outlet, doudoune canada goose, karen millen, converse, lancel, canada goose outlet, coach outlet, gucci, ray ban, thomas sabo, moncler, supra shoes, canada goose uk, juicy couture outlet, links of london, barbour jackets, louis vuitton, canada goose, barbour, ugg boots uk, pandora charms, bottes ugg, converse outlet, moncler, montre pas cher, canada goose, ugg pas cher, moncler
ninest123 16.03

menna said...

شركة تنظيف ستائر بالبخار بالشارقة مع التعقيم
شركة تنظيف بالبخار بالشارقة