- MVC_JSON.zip
- MVC_JSON
- Common
- ConsoleApplication1
- MvcApplication1.sln
- MvcApplication1.suo
- packages
- HttpClient.0.5.0
- lib
- JsonValue.0.5.0
- JsonValue.0.5.0.nupkg
- lib
- 40
- Microsoft.Json.dll
- Microsoft.Server.Common.dll
- repositories.config
- RestfulWebSite
- App_Data
- bin
- Common.dll
- Common.pdb
- Microsoft.Json.dll
- Microsoft.Server.Common.dll
- RestfulWebSite.dll
- RestfulWebSite.pdb
- Content
- Site.css
- themes
- base
- images
- ui-bg_flat_0_aaaaaa_40x100.png
- ui-bg_flat_75_ffffff_40x100.png
- ui-bg_glass_55_fbf9ee_1x400.png
- ui-bg_glass_65_ffffff_1x400.png
- ui-bg_glass_75_dadada_1x400.png
- ui-bg_glass_75_e6e6e6_1x400.png
- ui-bg_glass_95_fef1ec_1x400.png
- ui-bg_highlight-soft_75_cccccc_1x100.png
- ui-icons_222222_256x240.png
- ui-icons_2e83ff_256x240.png
- ui-icons_454545_256x240.png
- ui-icons_888888_256x240.png
- ui-icons_cd0a0a_256x240.png
- jquery-ui.css
- Controllers
- Filter
- Global.asax
- Global.asax.cs
- ModelBinders
- obj
- Debug
- packages.config
- Properties
- RestfulWebSite.csproj
- RestfulWebSite.csproj.user
- Scripts
- Views
- Web.config
- Web.Debug.config
- Web.Release.config
- WEB_WCF.zip
- WEB_WCF
- Lib
- WCF Web API Preview 4
- Microsoft.ApplicationServer.Common.dll
- Microsoft.ApplicationServer.Common.XML
- Microsoft.ApplicationServer.Http.dll
- Microsoft.ApplicationServer.Http.XML
- Microsoft.ApplicationServer.HttpEnhancements.dll
- Microsoft.ApplicationServer.HttpEnhancements.Tests.dll
- Microsoft.ApplicationServer.HttpEnhancements.XML
- Microsoft.ApplicationServer.Serialization.dll
- Microsoft.ApplicationServer.Serialization.XML
- Microsoft.ApplicationServer.ServiceModel.dll
- Microsoft.ApplicationServer.ServiceModel.xml
- Microsoft.Net.Http.dll
- Microsoft.Net.Http.xml
- Microsoft.QueryComposition.dll
- Microsoft.Runtime.Serialization.Json.dll
- Microsoft.Runtime.Serialization.Json.xml
- Microsoft.ServiceModel.Web.jQuery.dll
- Microsoft.ServiceModel.Web.jQuery.xml
- Models
- RestFul_Test
- RESTFul_WCF
- WEB_WCF_TEST.sln
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Web.Script.Serialization;
using Common;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
internal static DataContractJsonSerializer jss;
private static WebClient client = new WebClient();
private static NameValueCollection values = new System.Collections.Specialized.NameValueCollection();
static void Main(string[] args)
{
//Use ModelBinding in WebSite to resolve types to Controller Actions
Person pers = new Person(1, "name", null);
AddValue(values,"person", pers);
//NOTE : WebClient doesn't allow you to set ContentType header to "application/json"
// which is a shame as it would be very nice. But since the WebClient allows
// you to add anything to its collection of values, it makes sense to restrict the
// WebClient ContentType header to "application/x-www-form-urlencoded" (the default)
// as you can't really know what data you are sending, could be anything being sent up
// in one go, so what ContentType could be reasonably provided except
// "application/x-www-form-urlencoded"
// Also the WebClient is kind of a dumbed down HttpRequest, so its more limited, but also
// more convenient
Byte[] results = client.UploadValues("http://localhost:8300/Rest/Index", values);
//get results
List<Person> people = GetValue<List<Person>>(results);
Console.ReadLine();
}
internal static T GetValue<T>(Byte[] results) where T : class
{
using (MemoryStream ms = new MemoryStream(results))
{
jss = new DataContractJsonSerializer(typeof(T));
return (T)jss.ReadObject(ms);
}
}
internal static void AddValue(NameValueCollection values, string key, object value)
{
jss = new DataContractJsonSerializer(value.GetType());
using (MemoryStream ms = new MemoryStream())
{
jss.WriteObject(ms, value);
string json = Encoding.UTF8.GetString(ms.ToArray());
values.Add(key, json);
}
}
}
}
|
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.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog