I want to send a post request to a WCF rest service as you can see:
Guid id;
id = Guid.NewGuid();
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:47026/NewsRepository.svc/AddNews");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
id = id,
Subject="wfwf",
ViewerCounter="1",
Content="fsdsd",
SubmitDatatime="2012/12/12",
ModifiedDateTime="2012/12/12",
PublisherName="sdaadasd",
PictureAddress="adfafsd",
TypeOfNews="adsadaad"
});
streamWriter.Write(json);
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}
But I get an error - 400 bad request. So I traced my WCF log file and I found this error:
There was an error deserializing the object of type CMSManagement.Domain.Entity.News. DateTime content '2012/12/12' does not start with '/Date(' and end with ')/' as required for JSON.
After my dear friend @svek code. The result is like this:
Guid id;
id = Guid.NewGuid();
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:47026/NewsRepository.svc/AddNews");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string json = JsonConvert.SerializeObject(new
{
id = id,
Subject = "wfwf",
ViewerCounter = "1",
Content = "fsdsd",
SubmitDatatime = "2012/12/12",
ModifiedDateTime = "2012/12/12",
PublisherName = "sdaadasd",
PictureAddress = "adfafsd",
TypeOfNews = "adsadaad"
}, microsoftDateFormatSettings);
streamWriter.Write(json);
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}
But I get the same error. Why?
Instead of using the
string json = new JavaScriptSerializer().Serialize( new {...} );
use
//using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(new {...} );
Prior to Json.NET 4.5 dates were written using the Microsoft format: "/Date(1198908717056)/". If you want to use this format, or you want to maintain compatibility with Microsoft JSON serializers or older versions of Json.NET, then change the DateFormatHandling setting to MicrosoftDateFormat.
Source: http://www.newtonsoft.com/json/help/html/DatesInJSON.htm
// default as of Json.NET 4.5
string isoJson = JsonConvert.SerializeObject(data);
// { "MyDateProperty":"2009-02-15T00:00:00Z" }
Microsoft Date Format
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }; string microsoftJson = JsonConvert.SerializeObject(data, microsoftDateFormatSettings); // { "MyDateProperty":"\/Date(1234656000000)\/" }
string javascriptJson = JsonConvert.SerializeObject(data,
new JavaScriptDateTimeConverter());
// { "MyDateProperty":new Date(1234656000000)}
Here is the full working solution for your question:
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string json = JsonConvert.SerializeObject(new
{
id = id,
Subject = "wfwf",
ViewerCounter = "1",
Content = "fsdsd",
SubmitDatatime = "2012/12/12",
ModifiedDateTime = "2012/12/12",
PublisherName = "sdaadasd",
PictureAddress = "adfafsd",
TypeOfNews = "adsadaad"
}, microsoftDateFormatSettings); // ⇦ Added the format argument here
using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(streamWriter, json);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With