- 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.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Serialization;
using Microsoft.ApplicationServer.Http;
using Models;
namespace RestFul_Test
{
class Program
{
private enum MimeFormat { JSON, Xml };
static void Main(string[] args)
{
string uri = "http://localhost:8300/ef";
//GET Using JSON
HttpClient client = GetClient(uri, MimeFormat.JSON);
var response = client.Get(uri);
Console.WriteLine("=========GET==============");
foreach (Author author in response.Content.ReadAs<List<Author>>())
{
LogAuthorResultToConsole(author);
}
//POST using JSON
Console.WriteLine("\r\n");
Console.WriteLine("=========POST==============");
var newAuthor = new Author { Name = "Xml Barber" };
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri));
request.Content = new ObjectContent<Author>(newAuthor, "application/json");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
response = client.Send(request);
var receivedAuthor = response.Content.ReadAs<Author>();
LogAuthorResultToConsole(receivedAuthor);
Console.ReadLine();
}
private static void LogAuthorResultToConsole(Author author)
{
Console.WriteLine(string.Format(CultureInfo.InvariantCulture,
"\r\n Author Name: {0}, Author Id: {1}", author.Name, author.Id));
}
private static HttpClient GetClient(string uri, MimeFormat format)
{
var client = new HttpClient(new Uri(uri));
switch (format)
{
case MimeFormat.Xml:
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/xml"));
break;
case MimeFormat.JSON:
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
break;
}
return client;
}
}
}
|
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