Monday 16 December 2013

Calling WCF Service without adding web reference

if you want response in XML then use
request.ContentType = "application/XML";

and if you want response in JSON then use
request.ContentType = "application/JSON";

I found this code by googling on web. just see


var request = WebRequest.Create("http://localhost:81/Service1.svc/ReturnTime");
            request.Method = "GET";
            request.ContentType = "application/XML";          
            string responseFromServer = "No response from server";
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (var dataStream = response.GetResponseStream())
                {
                    if (dataStream != null)
                    {
                        using (var reader = new StreamReader(dataStream))
                        {
                            responseFromServer = reader.ReadToEnd();
                        }
                    }
                }
            }
            Console.WriteLine("Response: \n" + responseFromServer);

enjoy......

No comments:

Post a Comment