Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C# 4.0

Windows Communication Foundation and RESTful Web Services Primer

Rate me:
Please Sign up or sign in to vote.
4.79/5 (19 votes)
17 Feb 2010Ms-PL18 min read 95.2K   105  
Learn how to create and debug RESTful Web Services, and use streams and feeds.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Threading;

namespace WCFJsonPost
{
    internal class Program
    {
        internal static void Main(string[] args)
        {
            Thread.Sleep(10000);
            TestWcfRestPost();
        }

        private static void TestWcfRestPost()
        {
            // create the serializer that saves classes as JSON
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PostedData));
            PostedData dataToPost = new PostedData
                                        {
                                            FirstName = "Bart",
                                            LastName = "Simpson"
                                        };
            // set up our request
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://localhost:41000/MathService/web/data");
            request.Accept = "*/*";
            // we're going to post JSON
            request.ContentType = "application/json";
            request.Method = "POST";
            using (Stream stream = request.GetRequestStream())
            {
                // send data
                serializer.WriteObject(stream, dataToPost);
                stream.Flush();
            }

            // get the response
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using(Stream stream = response.GetResponseStream())
            {
                using(TextReader reader = new StreamReader(stream))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Technical Lead Kluwer Technology Services
Belgium Belgium
C# developer for a few years and specialized in an age past in C and Visual Basic 6. Having had the opportunity to work with .NET 3.5 since it was released, I've been using it with great success (big thanks to you LINQ team) to create functionality faster and better than was possible in 2.0, and am now looking into .NET 4.0. Currently I work at Kluwer Technology Services where I'm a technical lead, helping my fellows, architecting solutions and spreading the knowledge. I’m also an MCPD for .NET 2.0 and 3.5.

Comments and Discussions