|
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.
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.